Introduction
This article aims at understanding the various page navigation techniques available in ASP.NET, which technique should be used in which scenario and the pros and cons of using each technique.
Background
It is rarely the case that you have a website and the user would not want to navigate to other pages of your site. When a user wants to navigate multiple pages, then we will have to use one of the available navigation techniques in ASP.NET. Which navigation technique should be used depends on the current scenario. Let us try to explore various navigation techniques and understand them so that we can employ the right technique in our application.
Types of Navigation
There are various methods of performing navigation in ASP.NET:
- Client Side Navigation
- Client Side Browser Redirect
- Cross Page Posting
- Server Side Transfer
Let us now see all these methods in action. I have created a page to demonstrate each of these techniques. Let's call it the source page and from this page we will navigate to various target pages using various techniques.
Client Side Navigation
In this method, the markup of the rendered page contains links and information about the web page that it could navigate to. When the user chooses to navigate using this mark up (could be a hyperlink or button click), the browser takes him to the requested page.
This is often used and is perhaps the easiest way of navigation. Here the web page can simply provide a hyperlink to the user so that the user can click the hyperlink and navigate to other page.
The important thing to understand here is that no postback will take place if we are using this technique. So if we want to collect some data from page before taking the user to the next page. This might not be the right technique to use.
Cross Page Posting
In this method, we configure the controls in our web page such that the users' posted data will go to some other page instead of the original page.
This method is really useful when we have a page that will do the data gathering from the user and then pass it on to some other page for processing. Let us try to work an example here. We will ask the user to enter his name and then when he submits, we will take him to another page using this technique and will extract his name there and show a welcome message. To do that, we need to find the users' name that he entered on the last page.
if (Page.PreviousPage != null)
{
Label1.Text = ((TextBox)Page.PreviousPage.FindControl("TextBox1")).Text;
}
else
{
Label1.Text = "Stranger";
}
When we run the page.
The FindControl
and type casting on the navigated page looks risky and error prone. This can be circumvented by having strongly typed data in the form of public
properties on the previous page and accessing them to retrieve the data.
The important thing, when using this technique, is that no postback is happening to the original page but to the new page where we are navigating to.
Client Side Browser Redirect
This navigation method was initiated from server side. Some code running on the server will tell the users' browser to navigate to some other page.
This method is used when we have to decide the next page the user should see based on some conditions. So this decision will be taken on the server. Postback will happen and the server will tell the browser to take the user to the next page. Let's see how it is done.
Response.Redirect("ClientRedirect.aspx");

This is the most expensive method of doing navigation as it involves one extra round trip to server from browser (yet this seems to be most widely used method).
Server Side Transfer
In this method, the server will initiate the navigation like Client side browser redirect, but here the server code will simply hand over the control to some other web page so that the new page will then be rendered. Let us try to see how we can do that:
Server.Transfer("Serverside.aspx");

This method also solves the problem that Client side browser redirect had. It saves one round trip from browser to server. There is one thing to notice here, the URL is still not changed. That's because the browser had no idea that server has pushed another page for its request. Browser still thinks that it is displaying the old URL's page. (This might be an advantage or disadvantage.)
Points of Interest
We have seen multiple ways to provide navigation from one page to another. It is important to understand the working of each method so that we can take better decisions for what we need to use in our application.
History
- 23 Feb 2012: Understanding Page Navigation Techniques in ASP.NET