Comparison and selection of four page navigation methods for ASP.NET

zhaozj2021-02-16  71

Topic: ASP.NET four page navigation methods comparison and selection

| Reading: 118 | Release Date: 2004-6-24 In ASP.NET applications, there are a variety of ways between web forms: with a hyperlink, use response.redirect, with server.transfer, or with Server. EXECUTE. This article will analyze the four navigation methods and its excellent disadvantages to help you choose the best navigation. First, the superlink from a form into another form is the simplest way to use the HTML hyperlink control. In the web form, use hyperlink HTML code classes such as: When the user clicks the hyperlink, WebForm2.aspx executes and sends the result to the browser. Hyper link navigation is almost available anywhere, including HTML pages, and normal ASP pages. ASP.NET also provides another method of replaceable method, HyperLink server control:

Enter Form 2 The resulting 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 both have a little important difference, HyperLink Web Server Controls can be programmed on server-side. Specifically, it can be changed in the program code, allowing constructing a specific target to dynamically change the exits 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 This code is executed, if the user clicks on the link, he sees will be WebForm3.aspx, not Webform2.aspx. Second, use the program control redirection Although the hyperlink can navigate from one page to another, this navigation method is completely controlled by the user. Sometimes, we may have to use code to control the entire navigation process, including when to go to another page. In these cases, ASP.NET has three different ways to achieve similar purposes: call the REDIRECT method of the Response object, call the Transfer or Execute method of the Server object. These three navigation methods are basically similar, but there is also a difference. 2.1 Response.Redirect response.Redirect method causes the browser to link to a specified URL. When the response.redirect () method is called, it creates a response, indicating that the status code 302 (indicating the target has changed) and the new target URL. The browser receives the response from the server and uses the information in the response head to issue a request for the new URL.

That is to say, the redirection operation occurs in the client when using the response.redirect method, involving two communication with the server (two rounds): The first time is a request for the original page, get a 302 answer, second The second is the new page declared in the request 302 to get the page after redirect. 2.2 Server.Transfer Server.Transfer methods Turn the execution process from the current ASPX file to another ASPX page on the same server. When calling Server.Transfer, the current ASPX page is terminated, the execution process is transferred to another ASPX page, but the new ASPX page still uses the response created by the previous ASPX page. If you use the Server.Transfer method to implement the navigation between the page, the URL in the browser will not change because the redirection is completely on the server side, and the browser does not know that the server has already executed a page transformation at all. By default, the Server.Transfer method does not pass the form data or query string from one page to another, but as long as the second parameter of the method is set to true, you can retain the form of the first page. And query strings. At the same time, attention should be noted when using Server.Transfer: The target page will use the response created by the original page, which causes the ASP.NET's machine authentication check (Mac) that the viewState of the new page has been tampered with. Therefore, if you want to keep the form data and query string collection of the original page, you must set the enableViewStateMac property of the target page to false. 2.3 Server.execute Server.execute Method allows the current ASPX page to perform a specified ASPX page on the same web server. When the specified ASPX page is executed, the control process returns the location of the last Server.execute call. This page navigation mode is similar to a function call to the ASPX page. The page that is called can access the form data and query string collection that sent the call page, so set the enableViewStateMac property of the called page Page instruction to false. By default, the output called the page is appended to the current response. However, the Server.execute method has an overloaded method that allows the output of the called page to be called through a TextWriter object (or its sub-object, such as StringWriter object), not directly to the output stream, which is in the original page. It can be convenient to adjust the location of the resulting result of the called page.

To illustrate the working process, let's create a web form, put a button control (Button1) and a text control (Literal1), transfer to the code view in the design interface, join a system.io namespace of the Imports statement, then Add performed when the user clicks the button code: 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 creates a second page WebForm2.aspx for the same web application. Into the HTML page view, modify its ViewState Page Inhibit Check: <% @ Page Language = / vb / AutoEventWireup = / false / Codebehind = / WebForm2.aspx.vb / Inherits = / Navigate.WebForm2 / EnableViewStateMac = / false /%> Go to the design view, add some controls for the second page. Next, set the first page to the default page, start the application. Click the button, the WebForm2 control will display the place where the Literal button is placed in WebForm1, pay attention to the page title and URL still display the original page WebForm1. When you use the server.transfer or server.execute method, you should pay attention to a point: the final page may not be a legitimate HTML page, because the page that is finally returned to the client may contain multiple and and other tags. IE views seem to tolerate and handle this situation correctly, but if users want to use other browsers, it is best to test it carefully. Three, comparison and selection Since there are so many ways from a page to another, how should I choose the best navigation method? Here are some factors that need to be considered: · If you want to let users decide when to convert the page and transfer to which page, the hyperlink is best for the hyperlink. · If you want to control the target to control the target, the time to convert is determined by the user, dynamically set its navigateURL attribute using the HyperLink control of the web server. · Use the response.redirect if you want to connect the user to another server. · connects the user to non-ASPX resources with Response.Redirect, such as the HTML page. · If you want to keep 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, gain more Good performance and browsing effect.


New Post(0)