In ASP.NET applications, there are many ways to navigate between Web forms: using hyperlinks, using Response.Redirect, using Server.Transfer, or using Server.Execute. This article will analyze the similarities, differences, and advantages and disadvantages of these four navigation methods to help you choose the best navigation method.
1. Hyperlink
The simplest way to enter another form from one form is to use the HTML hyperlink control. In a Web form, use the HTML code class of hyperlinks such as:
<a href="WebForm2.aspx">Enter Form 2</a>
When the user clicks the hyperlink, WebForm2.aspx is executed and the result is sent to the browser. Hyperlink navigation can be used almost anywhere, including HTML pages and ordinary ASP pages. ASP.NET also provides another alternative method, 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 running result of the above HTML code is the same as the first example, because ASP.NET treats the HyperLink Web server control as an HTML super Link control. But there is one important difference between the two. HyperLink Web server controls can be programmed on the server side. Specifically, its NavigateUrl property can be changed in program code, allowing the construction of hyperlinks whose specific targets can change dynamically based on the current state of the application, for example:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
HyperLink1.NavigateUrl = "WebForm3.aspx"
End Sub
After this code is executed, if the user clicks on the link, he will see WebForm3.aspx instead of WebForm2.aspx.
2. Use program to control redirection.
Although hyperlinks can navigate from one page to another, this navigation method is completely controlled by the user. Sometimes, we may need to use code to control the entire navigation process, including when to go to another page. In these situations, ASP.NET has three different ways to achieve similar purposes: calling the Redirect method of the Response object, and calling the Transfer or Execute method of the Server object. The behavior of these three navigation methods is basically similar, but there are 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 with status code 302 (indicating that the target has changed) and the new target URL in the response headers. The browser receives the response from the server and uses the information in the response header to issue a request for the new URL.
That is to say, when using the Response.Redirect method, the redirect operation occurs on the client side and involves a total of two communications with the server (two round trips): the first is a request for the original page, which gets a 302 response, and the second The second time is to request the new page declared in the 302 response and get the page after the redirection.
2.2 Server.Transfer
The Server.Transfer method transfers the execution flow from the current ASPX file to another ASPX page on the same server. When Server.Transfer is called, the execution of the current ASPX page is terminated and the execution flow is transferred to another ASPX page, but the new ASPX page still uses the response stream created by the previous ASPX page.
If you use the Server.Transfer method to navigate 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 performed a page change.
By default, the Server.Transfer method does not pass form data or query strings from one page to another, but as long as the second parameter of the method is set to True, the form data of the first page can be preserved. and query string.
At the same time, one thing should be noted when using Server.Transfer: the target page will use the response stream created by the original page, which causes ASP.NET's Machine Authentication Check (MAC) to think that the ViewState of the new page has been tampered with. Therefore, if you want to retain the form data and query string collection of the original page, you must set the EnableViewStateMac property of the Page directive of the target page to False.
2.3 Server.Execute
The Server.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 flow returns to the location where the original page issued the Server.Execute call.
This page navigation method is similar to a function call for an ASPX page. The called page can access the form data and query string collection of the calling page, so the EnableViewStateMac property of the Page command of the called page must be set to False.
By default, the output of the called page is appended to the current response stream. However, the Server.Execute method has an overloaded method that allows the output of the called page to be obtained through a TextWriter object (or its sub-object, such as a StringWriter object), rather than appending directly to the output stream, so that in the original page You can easily adjust the location of the output results of the called page.
To illustrate its working process, let's create a Web form, put in a button control (Button1) and a text control (Literal1), switch to the code view in the design interface, add an Imports statement of the System.IO namespace, and then Add the code executed when the user clicks the button:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim sw As StringWriter = New StringWriter()
Server.Execute("WebForm2. aspx", sw)
Literal1.Text = sw.ToString()
End Sub
Then create a second page WebForm2.aspx for the same web application. Go to the HTML view of the page and modify its Page directive to disable ViewState checking:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
Inherits="Navigate.WebForm2" EnableViewStateMac="false "%>
Go to design view again and add some controls to the second page. Next, set the first page as the default page and start the application. Click the button, and the control of WebForm2 will be displayed where the Literal button is placed in WebForm1, as shown in Figure 1. Note that the page title and URL still display the original page WebForm1.
Figure 1: Use Server.Execute to merge two source files.
When using the Server.Transfer or Server.Execute method to implement navigation, one thing to note: the final page may not be a legal HTML page, because the page ultimately returned to the client May contain multiple <HTML> and <BODY> tags. Internet Explorer seems to tolerate and handle this situation correctly, but if you want to use another browser, you'd better test it carefully.
3. Comparison and selection
Since there are so many ways to navigate from one page to another, how should you choose the best navigation method? Here are some factors to consider:
·Hyperlinks are best if you want users to decide when to switch pages and which page to go to.
·If you want to use a program to control the conversion target, but the conversion timing is determined by the user, use the HyperLink control of the Web server to dynamically set its NavigateUrl property.
·If you want to connect the user to a resource 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 on the same Web server, you should use Server.Transfer instead of Response.Redirect, because Server.Transfer can avoid unnecessary network communication, thereby obtaining better performance and browsing effects.
· If you want to capture the output results of one ASPX page and then insert the results into another ASPX page at a specific location, use Server.Execute.
·If you want to ensure that the HTML output is legal, please use Response.Redirect, do not use the Server.Transfer or Server.Execute method.
This article is translated from: http://www.ondotnet.com/pub/a/dotnet/2003/04/07/aspnetnav.html