By default, buttons and other controls that cause sending back on ASP. NET web pages submit the page back to the page itself. This is part of the round-trip period that ASP. NET web pages will experience during normal processing. In some cases, you may need to send a page to another page. For example, you may be creating a multi-page form that collects different information on each page. In this case, you can configure some controls on the page (controls that implement the ibuttoncontrol interface, such as the button control) to be sent to different target pages. This is called cross-page sending. Cross-page sending has some advantages over redirecting to other pages using the transfer method.
Obtain information from the source page
When sending a configuration page for a cross-page request, you usually need to obtain information from the source page. This may include information from controls on the page (that is, information sent by the browser) and the Public attributes of the Source Page.
Get Control Value
The page class exposes a property named previouspage. If the source and target pages are in the same ASP. NET application, the previouspage attribute on the target page contains references to the Source Page. (The previouspage attribute is not initialized if the page is not sent across pages or is located in different applications .) By default, the previouspage attribute type is page.
If the source and target pages are in different applications, you cannot directly obtain the control value on the page, but you can read the sent data from the form dictionary. View status cannot be read from the source page because the source page is hashed. If you want to store values on the Source Page and make these values available to the target page of other applications, you can store these values as strings in hidden fields on the Source Page, request. form to access them.
By using the reference in the previouspage attribute, you can search for controls on the Source Page and extract the values of these controls. The findcontrol method is usually used to perform this operation.
if (Page.PreviousPage != null){ TextBox SourceTextBox = (TextBox)Page.PreviousPage.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; }}
The findcontrol method searches for controls in the current named container. If the control to be searched is located in another control (usually in the template), you must first obtain the reference to the container before you can find the control to be obtained in the container. In the following code example, the source page contains a login control and a layouttemplate container. The container also contains a Textbox Control named username. This code gets the value of the username control.
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.";}Obtain public property values from the Source Page
You can also obtain the value of a Public Member on the source page on the target page of the Cross-page message. The most common solution is to define public attributes on the Source Page and obtain the values of these attributes on the target page.
We recommend that you only disclose the required information as a public attribute to reduce the amount of information that may be used by Potential Malicious users.
To obtain a Public Member of the Source Page, you must first obtain a strong type reference to the Source Page.
You can perform this operation in multiple ways. The first method is to include a @ previouspagetype command on the target page, which allows you to specify the Source Page, as shown in the example:
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
When this command is included, the previouspage attribute is forcibly typed as the class of the referenced source page. Therefore, you can directly reference public members on the Source Page. You can use the type property to directly specify the type of the Source Page, or explicitly reference the source page in the virtualpath property to indirectly specify the type of the Source Page, as shown in this example.
The following code example shows a part of the Source Page, which contains a public property named currentcity, which is used to publish the value of the Textbox Control named textcity.
public String CurrentCity{ get { return textCity.Text; }}
The attribute created on the Source Page mainly used to send public values across pages is usually read-only. Although the Source Page can contain public read/write attributes, setting the Source Page attribute through the target page attribute does not have any effect, because this value is not retained.
If the target page contains the previouspagetype command directed to the Source Page, you can use the following code to access the currentcity attribute of the Source Page.
Label1.Text = PreviousPage.CurrentCity;
Another way to obtain a strong type reference to the Source Page is to include a @ reference command in the target page of the Source Page, just as any type of reference to be used on the page. In this case, you can obtain the previouspage attribute of the target page on the target page and forcibly convert it to the Source Page type, as shown in the following code example.
SourcePage_aspx sourcePage;sourcePage = (SourcePage_aspx) PreviousPage;Label1.Text = sourcePage.CurrentCity;
@ Reference Usage example download check target page for sending back
During cross-page sending back, the content of the Source Page Control is sent to the target page, and the browser performs the http post operation (not the get operation ). However, on the target page, after sending a cross-page message, the ispostback attribute becomes false immediately. Although this behavior is post, cross-page sending is not a sending back to the target page. Therefore, ispostback is set to false, and the target page can use its first code.
If this is useful in your application, you can determine whether the target page is running due to cross-page sending. To this end, you can test the iscrosspagepostback attribute referenced by the page returned by the previouspage attribute on the target page, as shown in the following code example.
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 sent across pages, the previouspage attribute returns NULL (nothing in Visual Basic ).
Cross-page transmission and server. Transfer
Previouspage attributes and previouspagetype commands are useful when calling the target page: in cross-page sending (this is a client-based transmission) and when using the transfer method (this is a server-based operation ). In the preceding two operations, the code on the target page can use the previouspage attribute to obtain references to the Source Page.
In the target page, it may be important to determine whether the page is sent across pages or the server. transfer operation call. To help you perform this operation, the page class discloses an attribute named iscrosspagepostback.