Accessing parent control from child control - ASP.NET C#
Try getting the child's NamingContainer.
Or you could iterate through the parents until you find the desired control, such as with an extension method.
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");
}
More details about this method here: http://www.teebot.be/2009/08/extension-method-to-get-controls-parent.html
@Rex M has a good and easy solution for this and just to expand on it to show the usage:
This code snippet is used from within the child user control to access parent user control property:
((MyParentUserControlTypeName)NamingContainer).Property1 = "Hello";