tags:

views:

56

answers:

3

How can I find the name of (default.aspx ) current page or web control in the code behind?

I want to write a superclass that uses this name.

+1  A: 

You mean that you want to find the original filename of the object that is currently executed? I.e., from inside a control MyControl you want to retrieve MyControlOnDisk.ascx? In general, this information is lost upon compiling, and moreover, many pages and controls are built on partial classes and the filenames they're from are compiled into a single assembly.

For a page, you can use the following, but only if the page is not internally redirected, is not instantiated as a class from another page, it is not a master page and you're not inside a static method:

string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;

In the case of a control, it is generally not possible as far as I know (it is compiled away), but perhaps someone can shed some light on this.

"i want to write a superclass that use this name "

I assume you mean to write a subclass? If you write a superclass you just create a virtual method and have it implemented in your subclass (the page). If you mean to create a subclass, you can take the classname of the page, which looks like this:

// current page
public partial class MyLovelyPage : System.Web.UI.UserControl

and use it like this to derive from it:

public partial class NewDerivedPage : MyLovelyPage
Abel
Can you give me an example where this is lost upon compiling? Due to the fact something needs to route to this handler, that's typically *not* the case in my experience.
Nick Craver
@Nick: the example will be any page. After compiling, the mapping between page and class is done inside the `*.aspx` file, which is read by the ASPX handler. The compiled class itself (which is in the bin-directory) will not contain any information of this. In fact, you can change the name of the path and not change the name of the class, whatever you see fit and do so at runtime. The only information that remains is in the `RuntimeCompatibilityAttribute` which stores the physical assembly name (which, partially, contains the name of the original file, depending of method of compilation).
Abel
@Nick: rephrased differently: this information isn't in the class at all to begin with. The mapping is done outside the class in a text-file (the ASPX file, web.config or another means) and the page is ignorant of how it was used or configured in Visual Studio (and can be reused with other pages).
Abel
@Abel - I think we're talking about different things...you seem to be trying to get the executing class name in which case yes that's not very straight-forward...we need a better definition of "current", rather than the `.aspx` file name, which is pretty straight-forward from the request and/or routing. Re-reading both your answer and the question, you're probably right, the first and second line of the question really don't go together, he seems to be after the handler class, not the page, my fault :)
Nick Craver
@Nick: I agree, the second line of the question doesn't seem to fit the first line. It'll be nice if @MHF sheds some light here...
Abel
actually i have a multi language website , all of my page(WebControl) inherits from my basePage (baseControl). in my base class i want to write a function (getValueFromKey) that load resource file of current page according to language (name of resource file and the page name is the same ) i want to write this function once that all developers can use it .
MHF
@MHF: multiple languages in dotnet should be resolved with resource dlls and satellite assemblies. That is fully automated, uses a standardized process and will save you a whole lot of trouble. See here: http://ondotnet.com/pub/a/dotnet/2002/10/14/local2.htm. Of course, if you want to try it yourself or need a special purpose approach, you can always do as you explain. In my post I showed how you can inherit and create a base class and how to get the filename.
Abel
A: 

if you not use Routing :

string sPath = HttpContext.Current.Request.Url.AbsolutePath;
        string[] strarry = sPath.Split('/');
        int lengh = strarry.Length;
        string sRet = strarry[lengh - 1]; 
Akyegane
A: 

Request.ServerVariables["SCRIPT_NAME"]

ace