Moving to Another Page

By default, when a page is submitted to the server, it is posted back to itself. However, there are many situations in a web application where you need to direct the application flow to another page, either directly or after posting to the server. There are four different ways to do this: HyperLink, Server.Transfer, Response.Redirect, and cross-page posting.

HyperLink

The Hyperlink control navigates directly to the location contained in the NavigateUrl property of the control without a postback to the server. This control is covered in Chapter 4.

Server.Transfer

The Transfer method of the HttpServerUtility class takes a URL of an .aspx or .htm page (but not .asp) as a string argument and posts back to the server. Execution of the current page is terminated and execution of the new page begins.

Because the HttpResponse.End method is called on the current page, it always raises a ThreadAbortException. This is usually not an issue, but if the Server.Transfer occurs as part of a connection-based database transaction (see Chapter 10 for details on connection-based database transactions) inside a try block, where the Rollback method is called in a catch block, the transaction will never commit because the transaction Commit method will be negated by the ThreadAbortException, unless you specifically catch the ThreadAbortException, as in the following code snippet:

try { // do database stuff,then Commit the transaction transaction.Commit(); // Navigate to another page assuming ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.