ASP. NET cross-page transfer methods

Source: Internet
Author: User
I. Response redirection method the response redirection method is the simplest method to redirect a webpage to another webpage so far. When the Web server receives a redirection 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 redirection 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. NET. The following code demonstrates how to use response. redirect Method for webpage redirection: protected void redirect_click (Object sender, eventargs e) {response. redirect ("menu. aspx ");} note that the redirection request is only a GET request, which means we cannot submit data through the redirection command from the source page. However, we can use a query string to transmit data in redirection. As shown in the following code: protected void redirect_click (Object sender, eventargs e) {response. Redirect ("menu. aspx? Username = "+ username. Text);} in the preceding example, a query string is passed as a parameter to the target URL of the response. Redirect method. We can use the following code to obtain the source data. Protected void page_load (Object sender, eventargs e) {string username = request ["username"];} 2. The server transmission method is different from the request sent from the client to another new page, server transmission is a server redirection technology. This technology simply changes the code processed by the Web server to request a new page. When the requested page and Source Page are on the same server, the server transmission ratio is response. the redirect method is more effective because this technology can avoid additional overhead and redirect only by using server resources. Note that this technology has a side effect. When the page is redirected, the client URL will still maintain the URL of the Source Page, this may make customers think that the data they obtain is generated on the Source Page. Of course, in most cases, this is not a problem, but it will make debugging more difficult. The server. transfer method can also save the httpcontext of the initial Page. Therefore, the value of the source page can be accessed on the target page. We can use the formscollection attribute to obtain the value of the source page from the target page. First, make sure that we use the overloaded method. This method has two parameters: the target URL and a Boolean value, telling the server whether to save the form used to describe the Source Page value. The following code is used: Server. transfer ("menu. aspx ", true); then, the code for getting the value of a Textbox Control named txtusername on the target page is as follows: Object OBJ = request. form ["txtusername"]; 3. response. redirect and server. transfer comparison due to response. the redirect method requires two request response operations. Therefore, we should avoid using this method for websites with high performance requirements. However, technically, using Redirect can indeed jump from one web page to another. In contrast, server. Transfer is more efficient, but the jump scope is limited to different webpages of the same web server. Essentially, we can use server. Transfer to eliminate unnecessary request response operations. If we need to relocate the webpage to different servers, we need to use the response. Redirect method. Iv. Cross-Page Submission overview in ASP. NET 2.0, we can implement the ibuttoncontrol interface to submit to different webforms for cross-Page Submission. Similar to response. Redirect, cross-page submission is a client-based transmission mechanism, but it is also a bit like server. transfer. The target webpage can also access the data of the source webpage. To use cross-Page Submission, We need to specify the target URL in the postbackurl attribute of the source webpage. 5. Cross-Page Submission will discuss how to implement cross-Page Submission in ASP. net2.0. In order to start learning, we suppose there are two web pages, one is the source web page and the other is the target Web page. The cross-Page Submission operation using buttons is initialized on the source webpage. First, we must set the postbackurl attribute of the target webpage button. By the way, all Web controls that implement the system. Web. UI. webcontrols. ibuttoncontrol interface have the cross-page submission feature. The following code demonstrates this process. Postbackurl = "~ /Target. aspx "text =" post to a target page "/> when we set the postbackurl attribute, Asp. net Framework binds the corresponding control to a new JavaScript function called webform_dopostbackwithoptions. The generated HTML code is as follows: onclick = "javascript: webform_dopostbackwithoptions (New webform_postbackoptions (" btnsubmit ","", false, "", "target. aspx ", false, false)" id = "btnsubmit"/> for the preceding HTML code, When you click the button, the browser submits the target URL (target. aspx) instead of the source URL. 6. Obtain the control value of the source page from the target page ASP. net2.0 provides a new property called previouspage, which is directed to the Source Page No matter when the current page is submitted across webpages. Note that when the Source Page and target page are in different applications, this property contains null (This NULL does not mean uninitialized ). It should also be noted that when the target webpage accesses the previouspage attribute, the data on the source page can be obtained. ASP. NET runs at the time of loading and executes the Source Page. This will trigger the processchildrequest event. It also triggers the page_init event, page_load, and any other source page button clicking events. Therefore, we need to avoid accidental misoperations, so it is best to use the iscrosspostback attribute to determine whether a cross-page commit occurs. If the attribute value is true, the target webpage is called by submitting an action across webpages. If it is called in another way (such as a general request, response. Redirect, or a server. Transfer), the value of this attribute is false. The following example demonstrates how to use this attribute. If (previouspage. iscrosspagepostback) {// Execution Code }//******************************** * ********* the previouspage attribute is stored on the server. both transfer and cross-page submission can be used. In ASP. in net2.0, we can call server. after the transfer operation, use the previouspage attribute to obtain data on the Source Page. The Code is as follows: **************************************** * ** // {server. transfer ("menu. aspx ");} protected void redirect_click (Object sender, eventargs e) // In this receiving area, we can now obtain the data on the web page. The Code is as follows: protected void page_load (Object sender, eventargs e) {If (previouspage! = NULL) {textbox txtbox = (textbox) previouspage. findcontrol ("txtusername"); If (textbox! = NULL) string username = textbox. text; // other executable code} note that the above Code must convert the txtusername control to the textbox type to allow access to the value. 7. The previouspagetype attribute provides strong type access to the Source Page in cross-page operations. The following shows how to obtain the control value from the source page without any type conversion. The Code is as follows: <asp: textbox id = "txtusername" runat = "server"/> <asp: textbox id = "txtpassword" runat = "server"/> <asp: button id = "Submit" runat = "server" text = "login" postbackurl = "menu. aspx "/> note that you can click the button to redirect to a menu. ASP. You can use the following code to obtain the user name and password for this target page: 8. Save the view status. For cross-Page Submission, Asp. net2.0 is embedded with a hidden field named _ PostBack. This field contains view information about the source page, which is provided by the Source Page, contains a server control with a non-empty postbackurl attribute value. You can use the information in _ PostBack to obtain the view status information of the Source Page. The Code is as follows: if (previouspage! = NULL & previouspage. iscrosspagepostback & previouspage. isvalid) {textbox txtbox = previouspage. findcontrol ("txtusername"); response. write (txtbox. text);} check code used in the above Code to ensure that the previouspage attribute is not null. By the way, if the target page and Source Page are not in the same application, the value of this previouspage attribute is null. The iscrosspagepostback attribute is true only when cross-page submission is performed. This cross-page submission feature is ASP. net2.0 is one of the most powerful features. This technology allows you to submit data to another page on one page, and seamlessly operate data on the source page on the target page.
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.