Get the page name example in asp.net c#

In this article i will learn how to get the page name in asp.net. If you are using visual studio 2005 to 2010 then url show page name with extension. But if you are using vs2012 to vs2015 then do not show. Lets check the example of  get page name from page url.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get the page name program in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Get Page Name" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="lblresult" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
Code Behind
using System;
using System.IO;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string pagename = Path.GetFileName(Request.Path);
        lblresult.Text = pagename;
    }
}

Code generate the following output


Get the page name in asp.net c#

0 comments:

Post a Comment