This new feature means that asp.net2.0 developers currently have three alternative technologies to transfer data from one Web page to another. These three methods are: Response redirection, server-side transfer, and new cross-page commit features. We can already be familiar with the first two technologies, so we'll just review them briefly, and then we'll focus on learning how to use cross-page submission features, and how this approach differs from response redirection and service delivery.
First, response redirection method
The response redirection method is the simplest way to redirect one Web page to another Web page so far. When the Web server receives a redirect request, it sends a response header to the client, which causes the client to send a new request to the server. In other words, a redirect request is actually two request responses: one is the initial request response and the other is the new Redirect request response.
It is easy to implement redirection in ASP. The following code demonstrates how to use the Response.Redirect method to implement Web page redirection:
protected void Redirect_click (object sender, EventArgs e) { Response.Redirect ("menu.aspx " ); }
Note that the redirect request is just a GET request, which means that we cannot submit the data from the source page through the redirect command. But we can use the query string to pass the data in the redirect. As shown in the following code:
protected voidRedirect_click (Objectsender, EventArgs e) {Response.Redirect ("menu.aspx?username="+username.text)); The above example passes a query string as a parameter to the destination URL of the Response.Redirect method. We can get the source data by the following code. protected voidPage_Load (Objectsender, EventArgs e) { stringUserName = request["UserName"]; }
Second, the server transmission method
And depending on the client sending requests to another new page, server transport is a service redirection technology that achieves the purpose of requesting a new page by simply altering the code that the Web server handles. When the requested page and the source page are on the same server, the server transfer is more efficient than the Response.Redirect method, because the technology avoids the extra overhead and can be redirected simply by using the server's resources. One of the side effects of this technique is that when the page is redirected, the client's URL remains the URL of the source page, which may cause the customer to assume that the data they have obtained is generated from the source page. Of course, this is not a problem in most cases, but it makes debugging more difficult.
The Server.Transfer method can also save the HttpContext of the initial page. Therefore, the target page can access the value of the source page. We can use the FormsCollection property to get the value of the source page from the target page. First, to make sure that we used the overloaded method, this method has two parameters: the target URL and a Boolean value that tells the server whether to save the form that describes the source page value. As shown in the following code:
Server.Transfer ("Menu.aspx", true);
We then get a code for the value of a TextBox control called txtUserName in the target page as follows:
Object obj = request.form["txtUserName"];
Iii. comparison of Response.Redirect and Server.Transfer
Since the Response.Redirect method requires two request response operations, we should try to avoid using this method on sites where performance requirements are high. However, only technically, using the Redirect method can actually jump from one Web page to another. In contrast, Server.Transfer will be more efficient, but the range of jumps is limited to different pages of the same Web server. In essence, we can use Server.Transfer to eliminate unnecessary request-response operations. If we need to relocate to a different server page, we need to use the Response.Redirect method.
Iv. Overview of cross-page submissions
in ASP. NET 2.0, we can implement cross-Web submissions by implementing the IButtonControl interface to different WebForm. Similar to Response.Redirect, cross-web submissions are a client-based transport mechanism, but also somewhat like Server.Transfer, where the target Web page can also access data from the source Web page. In order to use cross-page submissions, we need to specify the destination URL in the PostBackUrl property in the source Web page.
V. Implementation of cross-page submissions
This section discusses how to implement cross-page submissions in asp.net2.0. To begin our study, suppose there are two Web pages, one source Web page and the other is the target Web page. Cross-page commit operations using buttons are initialized in the source Web page. We must first set the PostBackUrl property of the target page button, and by the way, all Web controls that implement the System.Web.UI.WebControls.IbuttonControl interface have features that cross-page commits. The following code demonstrates this process.
Postbackurl= "~/target.aspx" text = "Post to a target page"/>
When we set the PostBackUrl property, ASP. NET Framework binds the appropriate controls to a new JavaScript function called Webform_dopostbackwithoptions, resulting in the following HTML code:
onclick= "Javascript:webform_dopostbackwithoptions (New Webform_postbackoptions (" Btnsubmit "," "", False, "", " target.aspx", False, False)) " Id= "Btnsubmit"/>
For the above HTML code, when the user clicks the button, the browser submits the destination URL (target.aspx) instead of the source URL.
Vi. getting the value of the source page control from the target page
ASP.NET2.0 provides a new property called PreviousPage, which points to the source page whenever the current page commits a cross-page commit operation. It is important to note that when the source and destination pages are in different applications, this property contains null (this null is not an uninitialized meaning). It is also important to note that when the target page accesses the PreviousPage property, the data from the source page can be obtained, ASP. The net runtime loads and executes the source page. This will cause the Processchildrequest event to occur. Also, it raises Page_Init events, Page_Load, and any other source page button click events.
Therefore, we want to avoid careless operation by mistake, so it is best to confirm whether a cross-page commit occurs with the Iscrosspostback property, if this property value is true, then the target page is called through a cross-page commit action. If it is called in a different way (such as a general request, Response.Redirect, or a server.transfer), the value of this property is false. The following example shows how to use this property.
if(previouspage.iscrosspagepostback) {//Execute Code}//******************************************This previouspage property can be used in both Server.Transfer and cross-page submissions. In asp.net2.0, we can use the PreviousPage property on the target page to get the data from the source page after invoking the Server.Transfer operation, with the following code:*******************************************//{Server.Transfer ("menu.aspx"); }protected voidRedirect_click (Objectsender, EventArgs e)//in this receive surface we can now get the data of the Web page, the code is as follows: protected voidPage_Load (Objectsender, EventArgs e) {if(PreviousPage! =NULL) {TextBox Txtbox=(TextBox) Previouspage.findcontrol ("txtUserName"); if(TextBox! =NULL)stringUserName =TextBox.Text; //Other executable code}}
Note that the above code must convert the txtUserName control to a textbox type so that the values in it can be accessed.
Vii. Use of PreviousPageType
The PreviousPageType property provides a strong typing ability to access the source page in cross-page operations, so let's show how to get control values from the source page without any type conversion. The code is as follows:
<Asp:textboxID= "txtUserName"Runat= "Server" /> <Asp:textboxID= "Txtpassword"Runat= "Server" /> <Asp:buttonID= "Submit"Runat= "Server"Text= "Login"PostBackUrl= "Menu.aspx" />
Note that the Click button can be redirected to a target page called "menu.asp". This target page can use the following code to obtain the user name and password:
Viii. Saving view state
For cross-page submissions, asp.net2.0 embedded a hidden field called __postback that contains view information about the source page-that is, provided by the source page, which contains a server-side control with a non-null PostBackUrl property value. The target page can use the information in __postback to get the view state information for the source page. The code is as follows:
if (previouspage!=null && previouspage.iscrosspagepostback && previouspage.isvalid) { = Previouspage.findcontrol ("txtusername"); Response.Write (Txtbox.text); }
Check code to ensure that the PreviousPage property is not NULL is checked in the code above. By the way, if the target page and the source page are not in the same application, the value of this previouspage property is null. The Iscrosspagepostback property is true only if the cross-page commit operation is in progress.
This cross-page commit feature is one of the strongest features in asp.net2.0, which allows you to submit a page to another page and seamlessly manipulate the data on the source page on the target page.