How to: Pass Values Between ASP.NET Web Forms Pages
In an ASP.NET Web Forms application, if you redirect from one ASP.NET Web Forms page to another, you will frequently want to pass information from the source page to the target page. For example, you might have a page where users can select items to purchase. When users submit the page, you want to call another page that can process the information that the user has entered. (For information about ways to navigate from one page to another in an ASP.NET Web site, see How to: Redirect Users to Another Page.)
Note
This topic applies only to ASP.NET Web Forms pages. It does not apply to pages that you create using ASP.NET MVC (Model View Controller) or ASP.NET Web Pages.
A Visual Studio Web application project with source code is available to accompany this topic: Download.
You can pass information between pages in various ways, some of which depend on how the redirection occurs. The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web Forms page:
Use a query string.
Get HTTP POST information from the source page.
The following options are available only when the source and target pages are in the same ASP.NET Web application.
Use session state.
Create public properties in the source page and access the property values in the target page.
Get control information in the target page from controls in the source page.
Using a Query String
When you use a hyperlink or Response.Redirect to navigate from one page to another, you can add information in a query string at the end of the URL.
Note
Never pass sensitive data using a query string, because the information is visible to users and can easily be modified, thus representing a potential security risk.
For more information, see QueryString.
To use a query string to pass information
In the source Web Forms page when you specify the URL of the target page, include the information that you want to pass in the form of key-value pairs at the end of the URL. The first pair is preceded by a question mark (?) and subsequent pairs are preceded by ampersands(&), as shown in the following example:
https://contoso.com/products.aspx?field1=value1 https://contoso.com/products.aspx?field1=value1&field2=value2
In the target page, access query string values by using the QueryString property of the HttpRequest object, as shown in the following example:
Dim s As String s = Request.QueryString("field1")
String s = Request.QueryString["field1"];
Getting Post Information from the Source Page
When the source Web Forms page uses the HTTP POST action to navigate to the target page, you can retrieve posted values from the Form collection in the target page. Note that you can get only the post values; you cannot read the values of arbitrary controls on the page.
Security Note |
---|
Do not pass sensitive data in posted values. Although posted values are not as readily available to users as query strings are, they can be viewed and changed using commonly available browser tools. |
To get the values of controls from the source page in another application
In the source Web Forms page, include a form element that contains HTML elements (such as input or textarea) or ASP.NET server controls (such as TextBox or DropDownList controls) that post values when the form is submitted.
In the target page, read the Form collection, which returns a dictionary of name/value pairs, one pair for each posted value.
The following code example displays the ID and value of every posted control in the source page and displays the posted values in a label named Label1.
Note
Post information from an ASP.NET Web Forms pages includes the values of hidden fields, such as __VIEWSTATE, __EVENTTARGET, and __EVENTARGUMENT, which are used for internal processing in the page. The following code example excludes the values of posted fields that are named with a leading double underscore (__).
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim displayValues As New StringBuilder() Dim postedValues As NameValueCollection = Request.Form Dim nextKey As String For i As Integer = 0 To postedValues.AllKeys.Length - 1 nextKey = postedValues.AllKeys(i) If nextKey.Substring(0, 2) <> "__" Then displayValues.Append("<br>") displayValues.Append(nextKey) displayValues.Append(" = ") displayValues.Append(postedValues(i)) End If Next Label1.Text = displayValues.ToString() End Sub
void Page_Load(object sender, EventArgs e) { System.Text.StringBuilder displayValues = new System.Text.StringBuilder(); System.Collections.Specialized.NameValueCollection postedValues = Request.Form; String nextKey; for(int i = 0; i < postedValues.AllKeys.Length; i++) { nextKey = postedValues.AllKeys[i]; if(nextKey.Substring(0, 2) != "__") { displayValues.Append("<br>"); displayValues.Append(nextKey); displayValues.Append(" = "); displayValues.Append(postedValues[i]); } } Label1.Text = displayValues.ToString(); }
Using Session State
Information in session state is available to all ASP.NET Web Forms pages in the current application. However, session state takes server memory, and the information is stored until the session expires, which can be more overhead than you want for simply passing information to the next page. For more information, see How to: Save Values in Session State and How to: Read Values from Session State.
To use session state to pass information
In the source page, save the information that you want to pass in session state, as shown in the following example:
Session("field1") = "value1"
Session["field1"] = "value1";
In the target page, read the saved information from session state, as shown in the following example:
Dim field1 as String = CType(Session.Item("field1"), String)
string field1 = (string)(Session["field1"]);
Getting Public Property Values from the Source Page
If the source page and target page are both ASP.NET Web Forms pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the Transfer method, the target page can access public properties in the source page.
To get public property values from the source page
On the source Web Forms page, create one or more public properties and save the page.
The following code example shows a property named CurrentCity that exposes the value of a TextBox control named textCity.
Public ReadOnly Property CurrentCity() As String Get Return textCity.Text End Get End Property
public String CurrentCity { get { return textCity.Text; } }
Note
Properties on the source page that are created primarily to expose values for cross-page posting are usually read-only properties. Although the source page can contain public read/write properties, setting a source page property from the target page property generally has no purpose, because the value will not be persisted.
On the target Web Forms page, add a @ PreviousPageType page directive that points to the source page.
The following code example shows a PreviousPageType directive that references a source page named SourcePage.aspx.
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
The PreviousPageType directive causes the page's PreviousPage property to be typed to the source page class.
In target page code, use strongly typed members of the PreviousPage property to read the source code properties.
The following code example reads the value of the CurrentCity property that is defined in the source page.
Label1.Text = PreviousPage.CurrentCity
Label1.Text = PreviousPage.CurrentCity;
Getting Control Information from the Source Page in the Same Application
If the source page and target page are both ASP.NET Web Forms pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the Transfer method, you can read the values of controls on the source page while in the target page. You might use this strategy if the source page does not expose public properties containing the information you need.
To get the values of controls from the source page in the same application
On the target page, get a reference to the source page by using the target page's PreviousPage property, and then call the FindControl method to get a reference to the control you want.
The following code example gets the value of the source page's TextBox1 control and displays it in the control named Label1:
If Not Page.PreviousPage Is Nothing Then Dim SourceTextBox As TextBox SourceTextBox = CType(PreviousPage.FindControl("TextBox1"), _ TextBox) If Not SourceTextBox Is Nothing Then Label1.Text = SourceTextBox.Text End If End If
if (PreviousPage != null) { TextBox SourceTextBox = (TextBox) PreviousPage.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; } }
The FindControl method finds controls in the current naming container. If the control you are looking for is inside another control, you must first get a reference to the container, and then search the container to find the control that you want. A typical example of this situation is when the previous page is a master page and the control that you want to find is inside a ContentPlaceHolder control. The following example is similar to the previous one except that it assumes that TextBox1 is located in a ContentPlaceHolder control that is named ContentPlaceHolder1:
If Not Page.PreviousPage Is Nothing Then Dim placeHolder As Control = PreviousPage.Controls(0).FindControl("ContentPlaceHolder1") Dim SourceTextBox As TextBox = CType(placeHolder.FindControl("TextBox1"), TextBox) If Not SourceTextBox Is Nothing Then Label1.Text = SourceTextBox.Text End If End If
if (PreviousPage != null) { Control placeHolder = PreviousPage.Controls[0].FindControl("ContentPlaceHolder1") TextBox SourceTextBox = (TextBox)placeHolder.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; } }
For information about how to get a reference to a control when you do not have a reference to the naming container, see How to: Access Server Controls by ID.
See Also
Tasks
How to: Determine How ASP.NET Web Forms Were Invoked
Concepts
Redirecting Users to Another Web Forms Page