A typical Web application is a collection of Web pages linked to each other. You can use the HyperLink control to navigate to a different Web page when the hyperlink is clicked. If you need to navigate to a Web page programmatically, you can use the following methods provided by ASP.NET:
The Response.Redirect() MethodThe Response.Redirect() method causes the browser to connect to the specified URL. When the Response.Redirect() method is called, it creates a response whose header contains a 302 (Object Moved) status code and the target URL. When the browser receives this response from the server, it uses the header information to generate another request to the specified URL. When using the Response.Redirect() method, the redirection occurs at the client side and involves two round trips to the server. Using the Response.Redirect() method is recommended in the following cases:
The Server.Transfer() MethodThe Server.Transfer() method transfers the execution from the current ASPX page to the specified ASPX page. The path specified to the ASPX page must be on the same Web server and must not contain a query string. When this transfer occurs, execution of the current ASPX page terminates and control is transferred to another ASPX page. The new ASPX page still uses the response stream created by the prior ASPX page. The URL in the browser still shows the original page because the redirection occurs on the server side and the browser remains unaware of the transfer. When you want to transfer control to an ASPX page residing on the same Web server, you should use Server.Transfer() instead of Response.Redirect() because Server.Transfer() avoids an unnecessary round trip and provides better performance and user experience. The default use of the Server.Transfer() method does not pass the form data and the query string of the original page request to the page receiving the transfer. However, you can preserve the form data and query string of the original page by passing a true value to the optional second argument of the Server.Transfer() method. The second argument takes a Boolean value that indicates whether to preserve the form and query string collections.
The Server.Execute() MethodThe Server.Execute() method enables the current ASPX page to execute a specified ASPX page. However, the path to the specified ASPX page must be on the same Web server and must not contain a query string. After the specified ASPX page is executed, control transfers back to the original page from which the Server.Execute() method was called. This technique of page navigation is analogous to making a method call to an ASPX page. The called ASPX page has access to the form and query string collections of the calling page. Thus, for the reasons explained in the previous section, you must set the EnableViewStateMac attribute of the Page directive to false on the called ASPX page. By default, the output of the executed page is added to the current response stream. This method also has an overloaded version in which the output of the redirected page can be fetched in a TextWriter object instead of adding the output to the response stream. This helps you control where the output is placed on the original page.
|