views:

18

answers:

2

I am new to ASP.net and I am trying to save the state of some textboxes on redirect. I have a dashboard interface with drilldown. The main dashboard page has a date range option (textbox for both start date and end date) for the information displayed and I need to save the date range chosen by the user. I know that I can put the info into a session variable to use in the page that I am redirect to, but when I hit the 'go back' button on the drilldown page it returns to the main dashboard page and the textbox value is lost. I ahve also tried saving the viewstate, but could not get that to work either. What is the best approach to solving this problem?

edit: In case it matters, I am actually using the jQuery $(location).attr('href', url); to do the redirect because some of the chart objects I am using cover html or asp hyperlinks in IE.

+1  A: 

You're on the right track.

On your redirect page, you're checking for the session variable, right?

Why not check for it on your original page too?

I.e.: Original page Onload:

IF NOT String.isnullorempty(Session("date")) THEN
   me.start_date.selectedvalue = Session("date")
END IF
dave
Edit: Session data is persisting through redirect now. If I set var=Session["txtEndDate"] for example, it does fine. However, when I try txtEndDate.Text = Session["txtEndDate"] or txtEndDate=Session["txtEndDate"] or txtEndDate.Text = (string)Session["txtEndDate"] I get an error. So my question now is how to restore the date from a session variable.
Jacob Huggart
If you're trying to populate a textbox, then this should work:me.txtEndDate.text = Session("txtEndDate")that's really a basic answer though -- what does the error message say?
dave
I solved it with Session["txtEndDate"] = txtEndDate.Text, txtEndDate.Text = (string)Session["txtEndDate"]
Jacob Huggart
A: 

So to clarify your question: you need to persist the selected values of the Date textboxes for when the user navigates back to the page.

The reason why ViewState does not work is because you have navigated away from the page. The ViewState is transported or "remembered" by inserting it as base 64 encoded data in the page, so as soon as you leave the page it is gone forever.

What you need to do is persist the data to Session as you were. Then in the Page.Load event handler of the dashboard page you check for the presense of the saved info in Session, and if it is there then populate it back into the textboxes, otherwise use a default.

If you are going to use Session to communicate values between pages then i would suggest you use static constants as the key to each piece of info, something like this:

public class MySessionKeys 
{
    public static string DateStarted = Guid.NewGuid().ToString();

    public static string DateFinished = Guid.NewGuid().ToString();
}

and in the aspx page:

protected void Page_Loaded(object sender, EventArgs e) 
{
    Session[MySessionKeys.DateStarted] = myDateStartedTextBox.Text;
}
slugster
Thanks for the input. This looks like a great solution. I have my session variable making it through redirects. How do I restore the value to a textbox?
Jacob Huggart