This tutorial is brought to you by NetroStar, a website development company.
In ASP.NET there are few ways to pass data from one page to another one. If you're wandering why to do this one of the possible scenarios is when the user chooses some items in internet shop on one page and your web application opens another page with summary of these item, then you have to pass data from one page to another. Actually you'll find that passing data between various pages is quite common. In this tutorial I'll show you various options how you can achieve it.
Using Query String
It's a simplest way of passing data from one page to another and it's done simply by modifying URL address. Parameters in URL string are in the end of it preceded with quotation mark (?) and each one is separated with ampersand (&). To call another page in in ASP.NET you can use Response.Redirect() method like in the following example:
This will result in calling a page named "Page2.aspx" with parameters: name with value "John" and age with value 20.
In the target page, in this example in Page2.aspx we have to receive these values. To to this it's best to use Request.QueryString() method like in the following example:
Remember that you must be careful while using this method of passing data between pages. Text in URL string can be seen and modified as well by anyone so don't put there sensitive data. One of the advantages of query strings is that it can be used between totally different pages - they don't have to belong to the same web application.
Post Data from Source Page
When you receive a request generated by POST method you can retrieve data from target page using Request.Form collection. To use this method you have to use a control that creates POST request in your source page, otherwise it won't work.
In the page where you receive requests we'll iterate through Request.Form collection and take a look what information can we obtain using the following code:
Data in Request.Form will be different depending your source page and control that you have used to generate POST request so you can check the content of it using the piece of code like the one listed above.
The advantage of these method is the same like the previous one - source page and target page don't have to be from the same web application.
Getting Values from the Properties
In this method you have to create a public property in source page and this property can be later accessed from another page within the same application.
Firstly let's create a public property in source page:
This property will return always value of TextBox where user should put his or her name. Now we need to point the so-called previous page (ie. our source page) in target page using @PreviousPageType directive:
Now it's very simple to access property from the previous page in target page, all you have to do is use Page.PreviousPage property like in the following example:
Getting Data from Controls
Instead of using public properties it is also possible to get data directly from various controls using Page.PreviousPage property. To use this method you don't event have to modify source page, all you have to do is to get concrete control in target page using the following code:
In this example we're looking for a control named "TextBox1" in PreviousPage and then we display text from it in the label on the target page. Please note that if you want to get access to control that is within other control firstly you have to get reference to the first control and only then to the second one.
Using Session State
Session state is simply a dictionary with key-value pairs and you can read or write it whenever you need during duration of a session. Session is a time when one concrete user interacts with your web application. Session state unlike application state is separate for each user. Using session state is very easy, you can for example save data from a user control to it:
Then you can use this data in any other part of your application simply by referring to it like in the following example:
If you want to learn more about session state check out our previous tutorials.
Summing up there are various methods for passing data from one page to another. When you're choosing between them first thing you need to ask yourself is if the pages are from the same application. If not you can choose only from the first two methods, otherwise you can use all methods described in this tutorial.
This tutorial was brought to you by NetroStar, a Miami-based global web design