By default, buttons and other controls that cause a postback on an ASP.NET Web page submit the page back to itself. This is part of the round-trip cycle that ASP.NET Web pages go through as part of their normal processing. For details, see Introduction to ASP.NET Web Pages.
Under some circumstances, you might want to post one page to another page. For example, you might be creating a multi-page form that collects different information on each page. In that case, you can configure certain controls (those that implement the
Note |
---|
You can also use the |
Because cross-page posting is configured for individual controls, you can create a page that posts to different pages depending on which button the user clicks.
Getting Information from the Source Page
When you configure a page for cross-page posting, you frequently want to get information from the source page. This might include the information from controls on the page—that is, the information being posted by the browser—as well as public properties of the source page.
Getting Control Values
The
Note |
---|
If the source and target page are in different applications, you cannot directly get the values of controls on the page, but you can read the posted data from the |
Using the reference in the PreviousPage property, you can search for controls on the source page and extract their value. You typically do this with the
Note |
---|
If you are coding the source page specifically to be able to share information with target pages, an easier way to make control values available to the target page is to expose them as public properties. For details, see Getting Public Property Values from the Source Page later in this topic. |
The following code example shows how you can get the value of the TextBox1
control on the source page.
Visual BasicВ | Copy Code |
---|---|
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 |
C#В | Copy Code |
---|---|
if (Page.PreviousPage != null) { TextBox SourceTextBox = (TextBox)Page.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 (typically, inside a template), you must first get a reference to the container and then search the container to find the control you want to get. In the following code example, the source page contains a UserName
. The code gets the value of the UserName
control.
Visual BasicВ | Copy Code |
---|---|
Dim LoginControl As Login LoginControl = CType(PreviousPage.FindControl("Login1"), Login) If Not LoginControl Is Nothing Then Dim UserName As TextBox UserName = CType(LoginControl.FindControl("UserName"), TextBox) If Not UserName Is Nothing Then Label1.Text = UserName.Text End If Else Label1.Text = "Cannot find user name in Login control." End If |
C#В | Copy Code |
---|---|
Login LoginControl = (Login)PreviousPage.FindControl("Login1"); if (LoginControl != null) { TextBox UserName = (TextBox)LoginControl.FindControl("UserName"); if (UserName != null) { Label1.Text = UserName.Text; } } else { Label1.Text = "Cannot find user name in Login control."; } |
Getting Public Property Values from the Source Page
In the target page of a cross-page posting, you can also get the values of public members of the source page. The most common scenario is that the source page defines public properties and you want to get their values on the target page.
Security Note |
---|
It is recommended that you expose only the information you need as public properties to reduce the amount of information available to potentially malicious users. |
To get public members of the source page, you must first get a strongly typed reference to the source page.
You can do so in a number of ways. The first is to include an
В | Copy Code |
---|---|
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> |
When this directive is included, the PreviousPage property is strongly typed to the class of the referenced source page. As a consequence, you can directly reference public members of the source page. You can specify the type of the source page either directly, using a type attribute, or indirectly by explicitly referencing the source page in a VirtualPath attribute, as shown in the example.
The following code example shows a portion of a source page containing a public property named CurrentCity
that exposes the value of a TextBox control named textCity
.
Visual BasicВ | Copy Code |
---|---|
Public ReadOnly Property CurrentCity() As String Get Return textCity.Text End Get End Property |
C#В | Copy Code |
---|---|
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. |
If the target page contains a PreviousPageType directive that points to the source page, you can access the source page's CurrentCity
property using code such as the following.
Visual BasicВ | Copy Code |
---|---|
Label1.Text = PreviousPage.CurrentCity; |
C#В | Copy Code |
---|---|
Label1.Text = PreviousPage.CurrentCity; |
Another way to get a strongly typed reference to the source page is to include an
Visual BasicВ | Copy Code |
---|---|
Dim sourcePage As SourcePage_aspx sourcePage = CType(PreviousPage, SourcePage_aspx) Label1.Text = p.CurrentCity |
C#В | Copy Code |
---|---|
SourcePage_aspx sourcePage; sourcePage = (SourcePage_aspx) PreviousPage; Label1.Text = sourcePage.CurrentCity; |
Checking for Postbacks in the Target Page
During a cross-page postback, the contents of the source page's controls are posted to the target page, and the browser executes an HTTP POST operation (not a GET operation). However, in the target page, the
If it is useful in your application, you can determine whether the target page is running as the result of a cross-page post. To do so, you can test the
Visual BasicВ | Copy Code |
---|---|
If PreviousPage IsNot Nothing Then If PreviousPage.IsCrossPagePostBack = True Then Label1.Text = "Cross-page post." End If Else Label1.Text = "Not a cross-page post." End If |
C#В | Copy Code |
---|---|
if(PreviousPage != null) { if(PreviousPage.IsCrossPagePostBack == true) { Label1.Text = "Cross-page post."; } } else { Label1.Text = "Not a cross-page post."; } |
Note that if the current page is not the target of a cross-page post, the PreviousPage property returns null (Nothing in Visual Basic).
For more information, see How to: Determine How ASP.NET Web Pages Were Invoked.
Cross-Page Posting versus Server.Transfer
The PreviousPage property and the PreviousPageType directive are useful in two situations where you invoke the target page: in a cross-page postback, which is a client-based transfer, and with the Transfer method, which is a server-based operation. In both operations, code in the target page can get a reference to the source page using the PreviousPage property.
It might be important in the target page to determine whether the page was invoked from a cross-page posting or a Server.Transfer operation. To help you do this, the Page class exposes a property named IsCrossPagePostBack. For details, see How to: Determine How ASP.NET Web Pages Were Invoked.