[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-08-21
11
Medium Priority
?
786 Views
Last Modified: 2013-01-13
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?
0
Comment
Question by:grwallace
  • 6
  • 4
11 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38316290
Do you have any other code which may be changing this date?
0
 

Author Comment

by:grwallace
ID: 38316326
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
 

Author Comment

by:grwallace
ID: 38316331
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38319454
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
 

Author Comment

by:grwallace
ID: 38319844
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38328442
So you set the Session("CurrentDate") in the other page to some other date? Can you show that code?
0
 

Author Comment

by:grwallace
ID: 38328455
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38328551
So on next page load, you are expecting to see this date?
0
 

Author Comment

by:grwallace
ID: 38328559
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1500 total points
ID: 38328582
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
 

Author Comment

by:grwallace
ID: 38328602
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question