Click here to Skip to main content
13,145,417 members (89,642 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am having some trouble setting a date on the calendar. I have 2 calendars, when the user selects a day on calendar 1, I want calendar 2's selected date to be calendar 1's date + 364 days.

So if cal.1 is: 11/27/2012 then cal.2 is: 11/26/2013. I have some code here it works when i just set calendar2.selecteddate = calendar1.selecteddate, but not when i add the following code.

Protected Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs)
    Dim calendar1 As Calendar = CType(AgreeForm.FindControl("Calendar1"), Calendar)
    Dim calendar2 As Calendar = CType(AgreeForm.FindControl("Calendar2"), Calendar)
    calendar2.SelectedDate = calendar1.SelectedDate.AddDays("364")
End Sub

can anyone give me some help with this?
both are on a formview.
Posted 27-Nov-12 4:29am
Mathlab 27-Nov-12 10:52am
Does it need some sort of databind to update calender 2?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Instead of the Add days, because I believe you always want the day previous in the coming year and you need to take into account leap years, perhaps you can try this approach. I think your main error is that you are sending in a string to .AddDays instead of a double as the method wants.

Just debug through each line of the code and ensure that what is supposed to be happening, is happening. If dtForCalendar2 is correct when you try to assign it to calendar2.SelectedDate, then the issue may be with your calendar2 markup. Could you use the improve question widget and add that markup to your question as well?
DateTime dtForCalendar2 = calendar1.SelectedDate
dtForCalendar2 = dtForCalendar2.AddYears(1.0)
dtForCalendar2 = dtForCalendar2.AddDays(-1.0)
calendar2.SelectedDate = dtForCalendar2
Member 8197993 27-Nov-12 11:48am
Marcus, I tried what you suggested, but it does not work.
below works perfect:
calendar2.SelectedDate = calendar1.SelectedDate

but it only duplicates what calendar1's selected date is. There has to be a way to get this to work, just havent figured it out yet.
Marcus Kramer 27-Nov-12 11:52am
When compound statements fail for me, I start by breaking them down into separate lines so that I can debug easier where the process is breaking. See my updated solution.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This worked for me:
Protected Sub Calendar1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.PreRender
    Calendar2.SelectedDate = Calendar1.SelectedDate.Date.AddDays(364)
    Calendar2.VisibleDate = Calendar2.SelectedDate
End Sub
Member 8197993 27-Nov-12 15:27pm
Nolensville, this is what i ended up doing to get this to work. I just havent had time to get back to post my answer here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100