Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

VB
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
Comments
[no name] 27-Nov-12 10:52am    
Does it need some sort of databind to update calender 2?

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?
VB
DateTime dtForCalendar2 = calendar1.SelectedDate
dtForCalendar2 = dtForCalendar2.AddYears(1.0)
dtForCalendar2 = dtForCalendar2.AddDays(-1.0)
calendar2.SelectedDate = dtForCalendar2
 
Share this answer
 
v2
Comments
PTG.Jake 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.
fjdiewornncalwe 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.
This worked for me:
VB
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
 
Share this answer
 
Comments
PTG.Jake 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900