Store URL and Go Back to Same Page From Where You Navigated in ASP.Net Application

Introduction

In today's article I will tell you how to store an URL and return to the same page from where you navigated in an ASP.NET application.

This article will show you how to store the URL of the previous page that will be displayed in the label at runtime. Also you will be able to return to the same page from where you navigated without using the browser's go back button.

Step 1

First of all I have added four WebPages to my application among which navigation will be done.

store url and go back to same page

These can be done by right-clicking on the application and choosing "Add New Item".

Step 2

Then I worked on one of the WebPages. I am working on the .aspx page, here I added three link buttons that will redirect to the rest of the pages.

    <div>

        <asp:LinkButton ID="lnkbtn1" runat="server" PostBackUrl="~/WebForm4.aspx" Text="page number 4"></asp:LinkButton>

        <br />

        <asp:LinkButton ID="lnkbtn2" runat="server" PostBackUrl="~/WebForm3.aspx" Text="page number 3"></asp:LinkButton>

        <br />

        <asp:LinkButton ID="lnkbtn3" runat="server" PostBackUrl="~/WebForm2.aspx" Text="page number 2"></asp:LinkButton>

    </div>

As you can see I passed the path of the rest of the pages in the PostBackUrl Property of the Link buttons.

Step 3

After this I added a HTML Link button that will redirect the user from where he was earlier.

        <div>

          <a href="javascript:history.go(-1)">Go back</a>

        </div>

For returning to one page we can use this single line of code of JavaScript: "javascript:history.go(-1)".

This will check the history and will take the user one page back.

Step 4

At the end I took the Label that will display the URL of the page from where we have just arrived.

        <div>

            <asp:Label ID="label1" runat="server"></asp:Label>       

        </div>

But now the question is, how will we store the URL in this Label?

So, for that we will go to the .aspx.cs page and write the following code in the page Load:

        protected void Page_Load(object sender, EventArgs e)

        {

            string referencepage = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;

            label1.Text = "You Came from:-" + referencepage;

          

        }

This code will get the URL of the previous page and will display it in the Label. As you can see, I took AbsoluteUri, this will return the complete URL of the page like: "You Came from:http://localhost:18388/WebForm4.aspx"  but if you want to display only the WebPage name then instead of AbsoluteUri you can use AbsolutePath.

Step 5

Now our work on the first page is completed, the same work is to be done on all the remaining WebPages.

The complete code of my WebPage is as follows:

.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication40.WebForm1" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script>

     

    </script>

</head>

<body>

    <form id="form1" runat="server">

        <b>You are at Page Number 1</b>

    <div>

        <asp:LinkButton ID="lnkbtn1" runat="server" PostBackUrl="~/WebForm4.aspx" Text="page number 4"></asp:LinkButton>

        <br />

        <asp:LinkButton ID="lnkbtn2" runat="server" PostBackUrl="~/WebForm3.aspx" Text="page number 3"></asp:LinkButton>

        <br />

        <asp:LinkButton ID="lnkbtn3" runat="server" PostBackUrl="~/WebForm2.aspx" Text="page number 2"></asp:LinkButton>

    </div>

        <br />

        <br />

        <div>

          <a href="javascript:history.go(-1)">Go back</a>

        </div>

        <br />

        <br />

        <div>

            <asp:Label ID="label1" runat="server"></asp:Label>       

        </div>

    </form>

</body>

</html>

.aspx.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication40

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string referencepage = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;

            label1.Text = "You Came from:-" + referencepage;

          

        }

 

    }

}

Output

Our application is created and is ready to be executed.

On running the application first your Home Page will be executed.

store url and go back to same page

But as you click on the Link Buttons for various pages then you will begin navigating from one page to another, also you will get the information of which page you have navigated.

store url and go back to same page

The same thing will happen when you click on the back button. It will take you to the page from where you navigated.

Up Next
    Ebook Download
    View all
    Learn
    View all