Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a custom control in Visual Studio and I need to somehow have access to the currently opened page in Visual Studio in design mode. If I look at the Page property of the custom control, the ID just says "__Page" and most of the properties are inaccessible which isn't very helpful. Does anyone know of a service or interface that has some method of obtaining the file name of the currently opened page in the project? Thanks.

share|improve this question
2  
Why would the control need to know anything about the page? – adam0101 Mar 2 '10 at 21:08

3 Answers 3

It CAN be done. See here: http://weblogs.asp.net/gunnarpeipman/archive/2008/05/21/using-parent-page-properties-in-user-control.aspx

but even this page starts out by saying

this situations is a warning sign - there's something wrong with UI structure if user controls should know their parents.

If you need to know information about the page, then a better approach is to create a property on your control that you can set from the page. Then from within the control, you can check the value of the property.

For example, if you REALLY need to know the page name, then your control should have a property called "PageName", and then when you ad it to your page, you can set the property explicitly. (I'll come back with an example if you like).

Here's an example I ran across - a date-time chooser control for a reporting page. It has a property called FromDate that is tied to a text box.

public  DateTime FromDate
{
    get
    {
        return Convert.ToDateTime(txtFrom.Text);
    }
    set
    {
        txtFrom.Text = value.ToString();
    }
}

Then you would set it int he page like this:

<uc1:DateRangeControl ID="DateRangeControl1" runat="server" 
                FromDate="1/1/2009"    />

The reason we say it's a design flaw is that good OO design would say that a control by nature should be able to operate without knowing what container it is in.

share|improve this answer
    
Thanks David +1. I certainly agree with you but this is not quite what I'm after. The end result of what I'm working on with these custom server controls won't cause any dependencies on containers or anything like that. I'm looking for the equivalent of a few properties generated by the run time context that show what the file name is that contains the current Page object. For example the AppRelativeVirtualPath property of the Page as a TemplateControl. It can also be found in the page Request and a few other places via reflection but these properties are all null during design time. – Nate Mar 3 '10 at 0:56

Why do you need this information at design-time? If you're having problems with it displaying correctly in design-mode, could you instead check the DesignMode property and only execute the page-dependent logic at runtime?

share|improve this answer
    
No I do not need the page file name information at run time. I would like to use the page file name information in some custom editors for server controls as a convenience for the developer. It will not create any dependencies for the controls. – Nate Mar 3 '10 at 18:02

I agree that if you need to do this, then there's probably something wrong with the UI design. However, I've found it useful on occasion.

Assuming your custom control extends WebControl or one of its subclasses and is inside a Page:

string myPageName = System.IO.Path.GetFileName(this.Page.Request.Path);

Easy as that.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.