Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Article

Understanding Page Navigation Techniques in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.58/5 (10 votes)
23 Feb 2012CPOL4 min read 71.5K   921   18   5
This articles discusses various page navigation techniques available in ASP.NET.

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.

Navigation Technique Image

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.

Navigation Technique Image

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.

C#
if (Page.PreviousPage != null)
{
    Label1.Text = ((TextBox)Page.PreviousPage.FindControl("TextBox1")).Text;
}
else
{
    Label1.Text = "Stranger";
}

When we run the page.

Navigation Technique Image

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.

C#
Response.Redirect("ClientRedirect.aspx");

Navigation Technique Image

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:

C#
Server.Transfer("Serverside.aspx");

Navigation Technique Image

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions

 
GeneralMy vote of 4 Pin
OmarIsaid13-Sep-16 3:12
OmarIsaid13-Sep-16 3:12 
AnswerArticle of the Day on Microsoft's site Pin
Rahul Rajat Singh10-Dec-12 18:39
professionalRahul Rajat Singh10-Dec-12 18:39 
QuestionNeed full code for Cross Page Posting Pin
Senthil Gopi28-Nov-12 21:21
Senthil Gopi28-Nov-12 21:21 
AnswerRe: Need full code for Cross Page Posting Pin
Rahul Rajat Singh28-Nov-12 22:37
professionalRahul Rajat Singh28-Nov-12 22:37 
I have only talked about the postbackurl method of cross page posting and the code is in the sample archive attached. there is a page named crosspage.aspx that posts all the values to the default.aspx by setting the postbackurl to default.aspx.
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.