Click here to Skip to main content
Click here to Skip to main content

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

Requirement

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)
    {
        Response.Redirect("~/Default.aspx");
    }

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)
    {
        Response.Redirect("~/Default.aspx");
    }

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.

Conclusion

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.

History

  • 3rd July, 2010: Initial version

License

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.

Comments and Discussions

 
GeneralMy vote of 2 Pinmembermakhaai7-Jul-10 9:49 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 4 Jul 2010
Article Copyright 2010 by Shining Legend
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid