Click here to Skip to main content
12,063,445 members (73,352 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.8K views
1 bookmarked
Posted

We Don't Usually Know What Kind of Parents Had Our Grandparents

, 5 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
(or How to Retrieve a Type of Parent Control Using an Extension Method)Here's a situation where I had to retrieve the next Parent Control of a certain type. In this particular case, my label is included in an UpdatePanel to update only when my Label is assigned a new value.I could have used the Pare

(or How to Retrieve a Type of Parent Control Using an Extension Method)

Here's a situation where I had to retrieve the next Parent Control of a certain type. In this particular case, my label is included in an UpdatePanel to update only when my Label is assigned a new value.

I could have used the Parent attributes of my controls and go up a few levels until I have the right one:

<textarea name="code" class="c#" cols="60" rows="10">
UpdatePanel updatePanel = label.Parent.Parent;
updatePanel.Update();

</textarea>

The ASP:

<textarea name="code" class="c#" cols="60" rows="10">

<asp:updatepanel id="UpdatePanelMessage" runat="server" updatemode="Conditional">

<asp:label id="LabelMessage" runat="server" resourcekey="labelErrorResource1">

</textarea>

Do you see what's wrong?

And what if the page is modified and the control is included in a new div or in a new panel? You see it coming, the control retrieved won't be an UpdatePanel because we don't usually know what kind of Parents had our Grandparents... So as soon as the asp page structure changes, chances are that casting errors start to appear.

We need a way to look up that control's ancestry and return the first parent of a certain type. We can achieve that by using the following extension method:

<textarea name="code" class="c#" cols="60" rows="10">
public static Control GetParentOfType(this Control childControl,
Type parentType)
{
Control parent = childControl.Parent;
while(parent.GetType() != parentType)
{
parent = parent.Parent;
}
if(parent.GetType() == parentType)
return parent;

throw new Exception("No control of expected type was found");
}
</textarea>

Now wherever we need to retrieve the first occurence of any control's parent of a certain type we just call the method GetParentOfType this way:

<textarea name="code" class="c#" cols="60" rows="10">

UpdatePanel updatePanel = (UpdatePanel)label.GetParentOfType
(typeof(UpdatePanel));
updatePanel.Update();

</textarea>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

teebot
Software Developer Logica
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160204.4 | Last Updated 5 Mar 2010
Article Copyright 2010 by teebot
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid