user466663
user466663

Reputation: 845

How to retain the previous page values in asp.net

I am working in asp.net webforms. I am collecting user inputs in a form, say Page1, containing several server controls suchas textboxes, comboboxes and radio buttons. When the user clicks on the save button, a new page, say Page2, will be displayed where all the values entered are displayed for verification. The Page2 is displayed by Server.Transfer("~/Page2") method in the button click event of Page1. In Page2, the user entered values are obtained by reading the PreviousPage property as shown below:

var memberData = PreviousPage.dependentData;

The Page2 has a Cancel button. When the user finds some dataentry error and want to go back to Page1, they could click on this button. The Cancel button click event runs the following code at the client side:

if (confirm('Are you sure to leave this page?')) {
                window.history.back(1);
            }

The problem is, when the Page1 is reached, all the user entered values are gone. Please let me know how to architect these two pages so that the values in the previous page (Page1) are retained.

Thanks

Upvotes: 2

Views: 5344

Answers (1)

Josh
Josh

Reputation: 44916

You have to save those values somewhere so that both pages have access to them. Most of the time this is done in a database, but you could also use Session, Cache, or even a Cookie if you wanted.

Since this is user specific, I suspect Session is your best bet. Create a simple data structure to match your data and save it into session:

public class UserData{
   public String Name {get; set;}
   public Int32 Age {get; set;}
}

Then when the user submits the page:

var userData = new UserData();
userData.Name = //Get from form;
userData.Age = //Get from form;

HttpContext.Current.Session["UserData"] = userData;

Then when loading your other page:

var userData = HttpContext.Current.Session["UserData"] as UserData;

if(userData != null){
   //Do some stuff!
}

Now... there are caveats with this mind you. For instance, if you are running this on multiple servers behind a load balancer, then you need to make sure you are using sticky sessions, or at least some kind of persistent Session Provider.

I guess what I am saying is do some homework before just slapping this into your app :)

Upvotes: 2

Related Questions