(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>