Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

how to get the page name?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ma - 28 Oct 2007 23:23 GMT
Hello,

  I want to get the name of current page. How can I do this?

For example if I am running the code from a page called deafult.aspx, it
should return me default.aspx and if I am running from a page called
mypage.aspx, it should return mypage.aspx. page.tostring() doesn't return
this value. Any suggestion?

Regards
Peter Bromberg [C# MVP] - 28 Oct 2007 23:34 GMT
There are a number of ways to do this including using the ServerVariables
collection and using Request.AbsoluteUrl and "chopping off" everything after
the last "/". But a ver easy way is to do:

Page.GetType().Name

that will return "YourPageName_aspx". You could replace the
underscore with a dot and you are done.
-- Peter
Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> Hello,
>
[quoted text clipped - 6 lines]
>
> Regards
John Timney (MVP) - 28 Oct 2007 23:41 GMT
This methods one approach

public string GetCurrentPageName()
{
   string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
   System.IO.FileInfo sPage = new System.IO.FileInfo(sPath);
   string sRet = sPage.Name;
   return sRet;
}

or you can parse the request.servervariables and split the returned string.

http://www.aspcode.net/List-of-RequestServerVariables.aspx

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog

> Hello,
>
[quoted text clipped - 6 lines]
>
> Regards
Juan T. Llibre - 28 Oct 2007 23:57 GMT
Even simpler than that :

VB.NET :
Dim pagename as string = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))

C# :
string pagename = System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]);

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
> This methods one approach
>
[quoted text clipped - 25 lines]
>>
>> Regards

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.