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.
|
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
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.
Then you would set it int he page like this:
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? |
|||||
|
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:
Easy as that. |
|||
|