In this article I will explain with an example, how to get Current Page and Previous Page names in ASP.Net using C# and VB.Net.
This article will illustrate how to extract Current Page and Previous Page names from URL using the Request.Url and Request.UrlReferrer properties in ASP.Net.
 
 

HTML Markup

Page1

The HTML Markup consists of following elements:
Anchor – For redirecting to Page2.
Label – For displaying name of the Current Page.
<a href="Page2.aspx">Redirect to Page 2</a>
<br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
 

Page2

The HTML Markup consist of following element:
Label – For displaying name of the Current Page and Previous Page.
<asp:Label ID="lblMessage" runat="server"></asp:Label>
 
 

Concept

Request.Url.Segments

The Request.Url property contains all the information about the Current Page.
The Request.Url.Segments property is an Array built by splitting the Current Page URL by Slash (/) character i.e. Segments of the URL.
The last element of the Array of Segments contains the name of the Current Page.
 
Request.UrlReferrer 
This is same as Request.Url.Segments but the only difference is that it contains all information about the Previous Page.
 
 

Getting Current Page and Previous Page names using C# and VB.Net

Current Page

Inside the Page_Load event handler, the last element i.e. Name of the Current Page is determined by subtracting the length of Segment Array of Request.Url property with 1.
Finally, the determined name are displayed using Label.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Get the Current Page Name.
        string currentPageName Request.Url.Segments[Request.Url.Segments.Length - 1];
        lblMessage.Text "Current Page: " + currentPageName;
    }
}
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        'Get the Current Page Name.
        Dim currentPageName As String Request.Url.Segments((Request.Url.Segments.Length - 1))
        lblMessage.Text "Current Page: " + currentPageName
    End If
End Sub
 

Previous Page

Inside the Page_Load event handler, the last element i.e. Name of the Previous Page is determined by subtracting the length of Segment Array of Request.UrlReferrer property with 1.
Finally, the determined names are displayed using Label.
Note: The Request.UrlReferrer property is NULL if there is no Previous Page and hence it is recommended to make use of NULL checking to avoid exceptions.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Get the Current Page Name.
        string currentPageName Request.Url.Segments[Request.Url.Segments.Length - 1];
        lblMessage.Text "Current Page: " + currentPageName + "<br /><br />";
 
        //Get the Previous Page Name.
        if (Request.UrlReferrer != null)
        {
            string previousPageName Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
            lblMessage.Text += "Previous Page: " + previousPageName;
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        'Get the Current Page Name.
        Dim currentPageName As String Request.Url.Segments((Request.Url.Segments.Length - 1))
        lblMessage.Text  "Current Page: " & currentPageName & "<br /><br />"
        
        'Get the Previous Page Name.
        If (Not (Request.UrlReferrer) Is Nothing) Then
            Dim previousPageName As String Request.UrlReferrer.Segments((Request.UrlReferrer.Segments.Length - 1))
            lblMessage.Text += "Previous Page: " & previousPageName
        End If
    End If
End Sub
 
 

Screenshot

Get Current Page and Previous Page names in ASP.Net  
 
 

Demo

 
 

Downloads