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.
  1. <div>  
  2.     <asp:LinkButton ID="lnkbtn1" runat="server" PostBackUrl="~/WebForm4.aspx" Text="page number 4"></asp:LinkButton>  
  3.     <br />  
  4.     <asp:LinkButton ID="lnkbtn2" runat="server" PostBackUrl="~/WebForm3.aspx" Text="page number 3"></asp:LinkButton>  
  5.     <br />  
  6.     <asp:LinkButton ID="lnkbtn3" runat="server" PostBackUrl="~/WebForm2.aspx" Text="page number 2"></asp:LinkButton>  
  7. </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 an HTML Link button that will redirect the user from where he was earlier.
  1. <div>  
  2.   <a href="javascript:history.go(-1)">Go back</a>  
  3. </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.
  1. <div>  
  2.     <asp:Label ID="label1" runat="server"></asp:Label>         
  3. </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:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     string referencepage = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;  
  4.     label1.Text = "You Came from:-" + referencepage;  
  5.     

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:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication40.WebForm1" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.      <head runat="server">  
  6.           <title></title>  
  7.           <script>  
  8.           </script>  
  9.      </head>  
  10.   
  11.      <body>  
  12.           <form id="form1" runat="server">  
  13.                <b>You are at Page Number 1</b>  
  14.                <div>  
  15.                     <asp:LinkButton ID="lnkbtn1" runat="server" PostBackUrl="~/WebForm4.aspx" Text="page number 4"></asp:LinkButton>  
  16.                     <br />  
  17.                     <asp:LinkButton ID="lnkbtn2" runat="server" PostBackUrl="~/WebForm3.aspx" Text="page number 3"></asp:LinkButton>  
  18.                     <br />  
  19.                     <asp:LinkButton ID="lnkbtn3" runat="server" PostBackUrl="~/WebForm2.aspx" Text="page number 2"></asp:LinkButton>  
  20.                </div>  
  21.                <br />  
  22.                <br />  
  23.                <div>  
  24.                     <a href="javascript:history.go(-1)">Go back</a>  
  25.                </div>  
  26.                <br />  
  27.                <br />  
  28.                <div>  
  29.                     <asp:Label ID="label1" runat="server"></asp:Label>  
  30.                </div>  
  31.           </form>  
  32.      </body>  
  33.   
  34. </html> 
.aspx.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.    
  8. namespace WebApplication40  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             string referencepage = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;  
  15.             label1.Text = "You Came from:-" + referencepage;  
  16.             
  17.         }  
  18.     }  

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.


Recommended Free Ebook
Similar Articles