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.
views:
98answers:
2It 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.
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?