Get Current HTML Page Name using JQuery and ASP.NET

This blog explains you how to get the current HTML Page Name using JQuery and Current Page Name in ASP.NET using C#
 
In some scenarios you will be in the situation to get the current page name in your application to perform some operation 
 
1. Get the HTML page name using JQuery 
 
Here is the code 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>Untitled</title>  
  6.   <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
  7. </head>  
  8. <body>  
  9. <button id="Btn_id">Click me to get Page Name</button>  
  10. <script>  
  11. $(document).ready(function(){  
  12. $('#Btn_id').click(function()  
  13. {  
  14. alert(document.location.href.match(/[^\/]+$/)[0]);  
  15. })  
  16. })  
  17. </script>  
  18.   
  19. </body>  
  20. </html>  
Result in browser:
 
 
 
2. Get the Current Page Name in ASP.NET using C#
 
Create a new web form in ASP.NET site and name it as PageName.aspx.
 
Here is my design in PageName.aspx. 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Untitled</title>  
  7.     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
  8. </head>  
  9.   
  10. <body>  
  11.     <button id="Btn_id">Click me to get Page Name</button>  
  12.     <script>  
  13.         $(document).ready(function() {  
  14.             $('#Btn_id').click(function() {  
  15.                 alert(document.location.href.match(/[^\/]+$/)[0]);  
  16.             })  
  17.         })  
  18.     </script>  
  19.   
  20. </body>  
  21.   
  22. </html>  
Code in PageName.aspx.cs
  1. protected void btn_Click(object sender, EventArgs e)  
  2. {  
  3.     string Page_Name = Path.GetFileName(Request.Path);  
  4.     lbl_pagename.Text = "Current Page Name is: " + Page_Name;  
  5. }  
Include System.IO Namespace to use the Path class.
 
Result in browser:
 
  
 
I hope you have enjoyed this blog.
 
Thank You 
Ebook Download
View all
Learn
View all