ASP.Net calendar control not saving date on return to page

I have a page with a calendar cotrol. When a date is selected another page is loaded, and the date is passed as a session variable wich is fine.
However when I return to the original page the calendar reverts to today's date. I have tried using the stored session variable to set the calendar back to the required date and it seems to work in the code. However when the page is rendered it is today's date which shows.

Any ideas anyone?
grwallaceAsked:
Who is Participating?
 
CodeCruiserCommented:
Ok. Is that problem for next month only or any month other than current? Lets try changing

Calendar1.SelectedDate = Session("CurrentDate")

to

Calendar1.SelectedDate = DateTime.ParseExact(Session("CurrentDate"), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)

And

Session("CurrentDate") = Calendar1.SelectedDate

to

Session("CurrentDate") = Calendar1.SelectedDate.ToString("dd/MM/yyyy")
0
 
CodeCruiserCommented:
Do you have any other code which may be changing this date?
0
 
grwallaceAuthor Commented:
no - in my Load event I have:-

 Private Sub WebForm1_Load
        If Session("BeenHereBefore") <> True Then
            Calendar1.SelectedDate = Today
            Session("BeenHereBefore") = True
            Session("CurrentDate") = Calendar1.SelectedDate
        Else
            Calendar1.SelectedDate = Session("CurrentDate")
        End If
        If Session("DoRebind") = True Then
            GridView1.DataBind()
            Session("DoRebind") = False
        End If
    End Sub

This correctly sets the date to today on load, but should not otherwise. I have set a breakpoint on it anyway and the code is working correctly
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
grwallaceAuthor Commented:
and on return calendar1.selecteddate is correctly set from the session variable. When the page renders, however, it shows the current date and all of the other controls are reset
0
 
Amandeep Singh BhullarCommented:
Your load method is the issue. Put page postback check and it will resolve the issue

If (Page.IsPostBack = False) Then
            If Session("BeenHereBefore") <> True Then
                Calendar1.SelectedDate = Today
                Session("BeenHereBefore") = True
                Session("CurrentDate") = Calendar1.SelectedDate
            Else
                Calendar1.SelectedDate = Session("CurrentDate")
            End If
            If Session("DoRebind") = True Then
                GridView1.DataBind()
                Session("DoRebind") = False
            End If

        End If



Hope this will help you....
0
 
grwallaceAuthor Commented:
Afraid it doesn't make any difference.
What I have noticed though is that it works provided that the month on the calendar is the current month - with or without the page.ispostback option
0
 
CodeCruiserCommented:
So you set the Session("CurrentDate") in the other page to some other date? Can you show that code?
0
 
grwallaceAuthor Commented:
no - session ("CurrentDate") only changes when the calendar control is clicked:-

 Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
        Session("CurrentDate") = Calendar1.SelectedDate
    End Sub
0
 
CodeCruiserCommented:
So on next page load, you are expecting to see this date?
0
 
grwallaceAuthor Commented:
On the next page it works fine. The only problem is that if the calendar is set to a different month on return to the first page it reverts to today's date and seems to ignore the statement:-

calendar1.selectedDate = session("CurrentDate")

If working on a date in the current month everything works great
0
 
grwallaceAuthor Commented:
No difference I'm afraid

The problme is for any month other than the current month or, to be more exact, if the month von the calendar control changes. The calendar control shows a full 6 weeks and provided the date is within that range and you don't change the month on the control it works perfectly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.