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.
1. hyperlink
<A href = "WebForm4.aspx"> WebForm4.aspx </a>
When the user clicks this hyperlink, WebForm4.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.
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. Response. Redirect
Response. Redirect ("WebForm4.aspx"); 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.
3. 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.
4. 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.
Summary
1. Server. Transfer can only jump to the page specified by the local virtual directory, while Response. Redirect is very flexible;
2. Server. Transfer can easily pass the page parameters to the specified page;
3. When Server. Transfer jumps to another page during use, the address displayed by the browser will not change, and sometimes it will lead to misunderstanding. Of course, this effect is also required in some occasions;
4. server. transfer can reduce client requests to the Server. (If you want to Transfer the execution process to another ASPX page of the same Web Server, you should use the Server. transfer instead of Response. redirect, because Server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance .)
5. If you want the user to decide when to convert the page and which page to go to, the hyperlink is the most suitable.
6. If you want to connect a user to resources on another server, use Response. Redirect.
7. 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
8. To ensure that the HTML output is valid, use Response. Redirect instead of Server. Transfer or Server. Execute.