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