Comparison and Selection of ASP. NET page navigation methods

Source: Internet
Author: User
In ASP. NET applications, there are multiple ways to navigate between web forms: Using Hyperlink, using response. Redirect, using server. Transfer, or using server. Execute. This article analyzes the similarities and differences between the four navigation methods and their advantages and disadvantages to help you select the best navigation method.
1. hyperlink
The simplest way to enter another form from a form is to use the HTML hyperlink control. In a web form, use the hyperlink html Code Class:

<A href = " Webform2.aspx " > Enter Form 2 < / A>

When the user clicks this hyperlink, webform2.aspx executes and sends the result to the browser. The hyperlink navigation method can be used almost anywhere, including HTML pages and common ASP pages. ASP. NET also provides an alternative method, that is, the hyperlink Server Control.

<Form ID = " Form1 " Method = " Post " Runat = " Server " >
<Asp: hyperlink ID = " Hyperlink1 " Runat = " Server "  
Navigateurl = " Webform2.aspx " > Enter Form 2 < / ASP: hyperlink>
< / Form>

The preceding HTML code runs the same result as the first example, because ASP. Net regards the hyperlink web server control as an HTML hyperlink control. But there is one important difference between the two. hyperlink web server controls can be programmed on the server side. Specifically, you canProgramThe code changes its navigateurl attribute to allow the construction of a hyperlink that can dynamically change according to the current state of the application. For example:

Private sub button#click (_
Byval sender as system. Object ,_
Byval e as system. eventargs )_
Handles button1.click
Hyperlink1.navigateurl =   " Webform3.aspx "  
End sub

After the code is executed, if the user clicks the link, the user will see webform3.aspx instead of webform2.aspx.
2. Use a program to control redirection 
Although hyperlink can navigate from one page to another, this navigation method is completely controlled by the user. Sometimes, we may use code to control the entire navigation process, including when to go to another page. In these cases, ASP. NET has three different methods to achieve similar goals: to call the Redirect method of the response object, to call the transfer or execute method of the server object. The behavior of these three navigation methods is similar, but there are also differences.

2.1 response. Redirect

The response. Redirect method causes the browser to link to a specified URL. When the response. Redirect () method is called, it creates a response. The response header indicates the status code 302 (indicating that the target has changed) and the new target URL. The browser receives the response from the server and sends a request to the new URL using the information in the response header.
This means that response is used. in the Redirect method, the redirection operation occurs on the client, involving two communications with the server (two back and forth) in total: the first request to the original page, and a 302 response is obtained, the second is the new page stated in the 302 response to the request, and the page after the redirection is obtained.

2.2 server. Transfer

The server. Transfer Method transfers the execution process from the current aspx file to another ASPX page on the same server. When server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page.
If you use the server. transfer method to implement navigation between pages, the URL in the browser will not change because the redirection is completely performed on the server side, and the browser does not know that the server has executed a page transformation.
By default, server. the transfer method does not pass form data or query strings from one page to another, but you only need to set the second parameter of this method to true, the form data and query string of the first page can be retained.
At the same time, use server. note that the target page will use the response stream created on the original page, which leads to ASP.. Net machine authentication check (MAC), The viewstate of the new page is deemed to have been tampered. Therefore, to retain the form data and query string set of the original page, you must set the enableviewstatemac attribute of the target page command to false.

2.3 server. Execute

Server. the execute method allows the current ASPX page to execute a specified ASPX page on the same web server. When the specified ASPX page is executed, the control process returns to the original page to issue the server. the location of the execute call.
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page, therefore, set the enableviewstatemac attribute of the called page command to false.
By default, the output of the called page is appended to the current response stream. However, server. the execute method has an overloaded method that allows a textwriter object (or its sub-object, such as the stringwriter object) to obtain the output of the called page, rather than directly appending it to the output stream, on the original page, you can easily adjust the location of the output result of the called page.
Use server. transfer or server. when using the execute method to implement navigation, note that the final page may not be a legal HTML page, because the page that is finally returned to the client may contain multiple <HTML> and <body> tags. Internet Explorer seems to be able to tolerate and handle such cases correctly, but if you want to use other browsers, you 'd better test it carefully.
Iii. Comparison and Selection
Since there are so many ways to navigate from one page to another, how should we select the best navigation method? The following are some considerations:
· If you want users to decide when to convert a page and which page to go to, hyperlink is the most suitable.
· If you want to use a program to control the conversion target, but the conversion time is determined by the user, use the hyperlink control of the Web server to dynamically set its navigateurl attribute.
· If you want to connect a user to resources on another server, use response. Redirect.
· Use response. Redirect to connect users to non-aspx resources, such as HTML pages.
· If you want to retain the query string as part of the URL, use response. Redirect.
· If you want to transfer the execution process to another ASPX page of the same web server, you should use server. transfer instead of response. redirect, because server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance.
· If you want to capture the output result of An ASPX page and insert the result to a specific location of another ASPX page, use server. Execute.
· To ensure that the HTML output is valid, use response. Redirect instead of server. Transfer or server. Execute.

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.