Sunday, November 14, 2010

Recursive FindControl for ASP.Net page

Page.FindControl method only checks the input ID from the immedidate child controls, while in most case the caller wants to get the control from multiple level of child control. It is more useful use the following method to find a control from a page.

public Control FindControlIterative(Control root, string id)
{

if (root.ID == id)
return root;
foreach (Control child in root.Controls)
{
Control childCtl = FindControlIterative(child, id);
if (childCtl != null)
return childCtl;
}


return null;
}

No comments:

Post a Comment