Asp. net page navigation several methods and comparisons are Shared

  • 2020-06-15 08:07:29
  • OfStack

1. Hyperlinks
The easiest way to get from one form to another is to use the HTML hyperlink control. In the Web form, the HTML code types that use hyperlinks are:

<a href="WebForm2.aspx"> In the form 2</a> 

When the user clicks the hyperlink, WebForm2.aspx executes and sends the result to the browser. Hyperlink navigation can be used almost anywhere, including HTML pages and regular ASP pages. ASP. NET also provides an alternative to using the HyperLink server control:

<form id="Form1" method="post" runat="server"> 
<asp:HyperLink id="HyperLink1" runat="server" 
NavigateUrl="WebForm2.aspx"> In the form 2</asp:HyperLink> 
</form> 

The above HTML code runs the same as in the first example, because ASP.NET treats the HyperLink Web server control as an HTML hyperlink control. There is one important difference, however. HyperLink Web server controls can be programmed on the server side. Specifically, you can change its NavigateUrl properties in the program code, allowing you to construct hyperlinks where specific targets can be dynamically changed based on the current state of the application, such as:

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. Program redirection
Although hyperlinks can be navigated from page to page, this navigation is completely controlled by the user. In some cases, we might want to use code to control the entire navigation process, including when to go to another page. In these cases, ASP.NET can achieve a similar purpose in three different ways: calling the Redirect method of the Response object, and calling the Transfer or Execute method of the Server object. The behavior of the three navigation methods is 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 reply with the header indicating the status code 302 (indicating that the target has changed) and the new target URL. The browser receives this reply from the server and USES the information in the reply header to make a request for the new URL.
That is, the redirect occurs on the client side using the Response.Redirect method and involves two communications with the server (two back and forth) : the first is the request for the original page, which results in a 302 reply, and the second is the request for the new page declared in the 302 reply, which results in the redirected page.
2.2 Server.Transfer
The Server.Transfer method moves the execution process from the current ASPX file to another ASPX page on the same 1 server. When Server.Transfer is called, the execution of the current ASPX page terminates and the execution flows to another ASPX page, but the new ASPX page still USES the reply flow created by the previous 1ASPX page.
If you implement navigation between pages using the ES69en.Transfer method, URL in the browser does not change, because the redirection takes place entirely on the server side and the browser has no idea that the server has performed a page change.
By default, the Server. Transfer method does not pass form data or query string from one page to another, but it retains the form data and query string for the first page as long as the second parameter of the method is set to True.
Also, note one point when using ES77en.Transfer: the target page will use the reply flow created by the original page, which causes the machine verification check of ES79en.NET (Machine Authentication Check, MAC) to assume that ViewState for the new page has been tampered with. Therefore, if you want to preserve the collection of form data and query strings for the original page, you must set the EnableViewStateMac attribute of the target page Page directive to False.
2.3 Server.Execute
The Server. Execute method allows the current ASPX page to execute one of the specified ASPX pages on the 1Web server, and when the specified ASPX page finishes executing, the control flow returns to where the original page made the Server. Execute call.
This page navigation is similar to one function call for the ASPX page, where the called page can access the collection of form data and query strings that issued the calling page, so the EnableViewStateMac property of the called PAGE Page directive is set to False.
By default, the output of the called page is appended to the current reply flow. However, the ES106en.Execute method has an overloaded method that allows the output of the called page to be fetched through an TextWriter object (or its children, such as the StringWriter object) rather than appended directly to the output stream, making it easy to reposition the output of the called page in the original page.
In order to illustrate its working process, we create an Web form, put 1 button control (Button1) and 1 text control (Literal1), enter the code view in the design interface, add 1 ES114en.ES115en namespace Imports statement, 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, ES121en2.aspx, for the same Web application. Go to the HTML view on this page and modify its Page directive to prohibit ViewState checking:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" 
Inherits="Navigate.WebForm2" EnableViewStateMac="false"%> 

Go to Design View again and add a few controls for the second page. Next, launch the application by setting the first page as the default page. Click the button and the WebForm2 control will show where the Literal button was placed in WebForm1. Note that the page title and URL still show the original page WebForm1.
One more thing to note when implementing navigation using the ES135en.Transfer or Server.Execute methods: The resulting page may not be a legitimate HTML page because the page returned to the client may contain multiple pages < HTML > and < BODY > Such as tags. The IE browser seems to tolerate and handle these situations properly, but if you're using another browser, it's a good idea to test 1 carefully.
3. Compare and choose
Since there are so many ways to navigate from page to page, how do you choose the best way to navigate? Here are 1 things to consider:
· If it is up to the user to decide when to switch pages and which pages to go to, hyperlinks are best.
· If you want to programmatically control the target of the transformation, but the timing of the transformation is up to the user, use the HyperLink control of the Web server to dynamically set its NavigateUrl properties.
· To connect a user to a resource on another server, use ES155en.Redirect.
· Use ES158en.Redirect to connect users to non-ES160en resources, such as HTML pages.
· If you want to keep the query string as part 1 of URL, use Response.Redirect.
· If you want to transfer the execution process to another ASPX page with the 1Web server, you should use ES169en.Transfer instead of ES171en.Redirect, as Server.Transfer can avoid unnecessary network communication for better performance and browsing results.
· Use Server.Execute if you want to capture the output of one ASPX page and then insert the result into a specific location of another ASPX page.
· To ensure that HTML output is valid, use Response.Redirect, not Server.Transfer or Server.Execute methods.

Related articles: