Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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 5:29am
Comments
Mathlab at 27-Nov-12 10:52am
   
Does it need some sort of databind to update calender 2?
Rate this: bad
good
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
  Permalink  
v2
Comments
Member 8197993 at 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 at 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
good
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
  Permalink  
Comments
Member 8197993 at 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
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
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