When we want to find a control by its ID, chances are it is usually nested in many levels. In this scenario, we need more powerful method than the .NET framework provided Control.FindControl.
Writing such a method actually is straightforward enough:
private static Control FindControlByID(Control root, string id)
{
if (id == string.Empty)
return null;
if (root.ID == id)
return root;
foreach (Control c in root.Controls)
{
Control t = FindControlByID(c, id);
if (t != null)
{
return t;
}
}
return null;
}
It works great no matter there is master page involved or not; no matter how many levels the control hierarchy has.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment