Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETVB.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 - 4:29

Comments
Mathlab - 27 Nov '12 - 10:52
Does it need some sort of databind to update calender 2?

2 solutions

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 - 27 Nov '12 - 15:27
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.
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  
Comments
Member 8197993 - 27 Nov '12 - 11:48
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:52
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 content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 253
1 Rohan Leuva 220
2 Sergey Alexandrovich Kryukov 208
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,494
1 OriginalGriff 6,799
2 CPallini 3,603
3 Rohan Leuva 2,923
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid