Groups | Blog | Home
all groups > asp.net building controls > july 2004 >

asp.net building controls : Child user control accessing parent properties


Michael
7/13/2004 1:40:18 PM
We have a user control (Titlebar) that loads other user controls (children)
into itself based on a property set in the HTML:

<fss:Titlebar id="empStmtListing" runat="server"
UserControl="StatementListing.ascx" Width="90"
HelpId="1" ListingType="3"></fss:Titlebar>

Some of the children user controls need properties set to execute. We would
put these properties in the HTML of the parent,
for example, the ListingType property above. Is there anyway that
StatementListing.ascx can access and read the ListingType
property? Or will our Titlebar user control always have to pass the
properties into the child control?

Thanks

--
Michael W. Wilson
Raytheon Company
Sr. Business Programmer

John Saunders
7/14/2004 12:15:46 AM
[quoted text, click to view]

It's really bad form for a child control to know too much about its parent.
Instead, let the parent tell the child what it needs to know.
--
John Saunders
johnwsaundersiii at hotmail

XicoLoKo
7/15/2004 2:30:02 AM
Another approach would be to have a base usercontrol class, lets say TitleBarChildControlBase, that exposes those properties from it's parent.

User controls that inherit TitleBarChildControlBase would be used only inside the TitleBar.

- xicoloko -

[quoted text, click to view]
John Saunders
7/15/2004 10:42:32 AM
[quoted text, click to view]
inside the TitleBar.

Ok, I originally read the OP as asking a question about user controls in
general. If, instead, the user controls in question will _always_ be used
inside of the same parent control, then knowing about their parent would be
ok.

In this case, it would be ok if the user controls were to cast their Parent
property to the type of the parent:

VB.NET:

Dim tbParentTitlebar As Titlebar = DirectCast(Parent, GetType(Titlebar))
' Can now use tbParentTitlebar.ListingType

C#:

Titlebar tbParentTitlebar = (Titlebar) Parent;
// Can now use tbParentTitlebar.ListingType
--
John Saunders
johnwsaundersiii at hotmail



[quoted text, click to view]

AddThis Social Bookmark Button