7.3. Programmatic Redirection

Programmatic redirection is very useful and common in ASP.NET pages. For example, imagine a page that allows users to enter a review into the database. As soon as they click the Save button, the review is saved and then users are taken to another page where they can see the entire review.

ASP.NET supports two major ways to redirect users to a new page programmatically: using Response.Redirect and Server.Transfer. Since both behave pretty differently, the following two sections describe their behavior in more detail.

7.3.1. Programmatically Redirecting the Client to a Different Page

Within each ASPX page you have access to a property called Response that you saw earlier when saving the cookie for the selected theme. The Response object gives you access to useful properties and methods that are all related to the response from the server to the user's browser. One of those methods is the Redirect method. This method sends an instruction to the browser to request a new page. This is useful if you want to redirect your user to another page in your site, or to a completely different web site. The Redirect method can be used in two different ways:

Response.Redirect(newUrl)
Response.Redirect(newUrl, endResponse)

The second version, which has an additional Boolean parameter called endResponse, allows you to execute any remaining code after the call to Response.Redirect when you pass False for the endResponse parameter. This is usually not necessary so you're ...

Get Beginning ASP.NET 3.5: In C# and VB 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.