Click here to Skip to main content
13,298,947 members (38,653 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 4 Jul 2010

Navigate to Previous Page in ASP.NET

, 4 Jul 2010
Rate this:
Please Sign up or sign in to vote.
How to navigate to the previous page in ASP.NET


There was a requirement to get the URL of the previous page from which I was redirected to the New page in the Button click event handler of the New Page, so that I can redirect the user to the previous page.

In this article, you will see how to get the URL of the previous page from which you were redirected to the new page.

We have to use UrlReferrer property of Request object to access the previous page URL. The UrlReferrer property is used to get information about the URL of the client's previous request that linked to the current URL.

Please follow the below steps for the requirement given above.

Step 1: Creating a New Website: PrvsPageDemo

  1. Open Visual Web Developer or Visual Studio 2008.
  2. On the File menu, click New Website, in the Dialog box that appears Under VisualStudioInstalled templates, select ASP.NET Website.
  3. Then Type the location (Foldername) where you want to create your website such as D:\ PrvsPageDemo in the second location box.
  4. Select the language you want to create a website in. Let us select C#.
  5. Click OK. This will create a website with the name PrvsPageDemo. This website by default contains a Webpage named Default.aspx.
  6. Add 2 more Webpages, namely Management.aspx and Development.aspx page to the website. (Go to View tab -> click on SolutionExplorer -> In the Solution Explorer window, right click on the Project name (PrvsPageDemo) -> Select Add New Item -> In the name Box: type Management.aspx and then click on Add. Similarly add Development.aspx page.)

Step 2: In the Default Page Source View, Add the Following Code under Form Tag.

This is the Default Page.

<asp:Button runat="server" ID="btnBack" Text="Back" onclick="btnBack_Click" />

Go to Default.aspx.cs file and add the following code as shown below:

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack) //check if the webpage is loaded for the first time.
            ViewState["PreviousPage"] = 
		Request.UrlReferrer;//Saves the Previous page url in ViewState
    protected void btnBack_Click(object sender, EventArgs e)
        if (ViewState["PreviousPage"] != null)	//Check if the ViewState 
						//contains Previous page URL
            Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to 
		//Previous page by retrieving the PreviousPage Url from ViewState.

Here, in the Default.aspx page, we have added some static text and a Back Button.

In the Default.aspx.cs page, in the PageLoad event, in !IsPostBack section, we are saving the value of the Request.UrlReferrer in viewState which is the previous page URL and using the same in the Back Button click event handler.

In the Button click, we first check if ViewState contains the previous page URL. If the ViewState contains the previous page URL, then we use the viewstate information to redirect the user back to the previous page.

Step 3: In the Management.aspx Page under Form Tag, Add the Following Code

This is the management Page.

<asp:Button ID="btnDefault" runat="server" Text="Go to Default Page" 

            onclick="btnDefault_Click" />

In the code-behind file (Management.aspx.cs), add the below code:

protected void btnDefault_Click(object sender, EventArgs e)

Here, when you click the button, User is redirected to Default.aspx page as written in the btnDefault_click event handler of the button.

Step 4: In the Development.aspx Page under Form tag, Add the Following Code

This is the Development Page.

<asp:Button ID="btnDefault" runat="server" Text="Go to Default Page" 

            onclick="btnDefault_Click" />

In the code-behind file (Development.aspx.cs), add the below code:

protected void btnDefault_Click(object sender, EventArgs e)

Here, when you click the button, the user is redirected to Default.aspx page.

Step 5: Press CTRL + F5 to Execute the Default Page

The Webpage (Default.aspx page) appears in the web browser. Click the Back button. Nothing happens.

Now view the Management.aspx page and then click on the button on this page which will redirect you to the Default Page. When you click on Back button on Default.aspx page now, it will redirect you to the Management.aspx page. Similarly try accessing Development.aspx page.


Using Request.UrlReferrer, we can get the Previous page URL of the current request. Previous Page information is available in the Request.UrlReferrer property only if user is redirected to the current page from some other page and is available in the !Page.IsPostBack of Page Load event of the form. Here we use ViewState because it can be used if you want to store the values on postback to the same form. ViewState is used for retaining values between multiple requests for the same page. This is the default method that the page uses to preserve page and control property values between round trips.


  • 3rd July, 2010: Initial version


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


About the Author

Shining Legend
Software Developer
India India
I am a Software Developer in Dotnet technologies. I also provide online training in dotnet technologies.

You may also be interested in...

Comments and Discussions

AnswerThis is not good idea, if page is refresh Pin
Member 314438510-Dec-13 20:43
memberMember 314438510-Dec-13 20:43 
GeneralMy vote of 2 Pin
Rufet_19-Aug-13 22:06
memberRufet_19-Aug-13 22:06 
QuestionUmmm... Pin
JasonMacD7-Jun-13 7:31
memberJasonMacD7-Jun-13 7:31 
GeneralMy vote of 3 Pin
sandeepkumarvemula14-Feb-13 23:46
membersandeepkumarvemula14-Feb-13 23:46 
GeneralMy vote of 5 Pin
Sohaib Ahmed11-Jan-13 0:23
memberSohaib Ahmed11-Jan-13 0:23 
GeneralRe automatic redirection to Login.aspx Pin
Ajay Kale New9-Sep-10 3:40
memberAjay Kale New9-Sep-10 3:40 
GeneralMy vote of 2 Pin
makhaai7-Jul-10 10:49
membermakhaai7-Jul-10 10:49 
My vote of 2
GeneralMy vote of 1 Pin
seva.feldman6-Jul-10 23:36
memberseva.feldman6-Jul-10 23:36 
GeneralMy vote of 2 Pin
Andrey Mazoulnitsyn5-Jul-10 20:31
memberAndrey Mazoulnitsyn5-Jul-10 20:31 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 4 Jul 2010
Article Copyright 2010 by Shining Legend
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid