Recursive method to find controls

Nishithraj
Posted by Nishithraj under ASP.NET category on | Views : 3447
Here is a method to find the controls by a recursive function. Through this method we can find out the control say an example as button.
if we require to find out the buttons of a page we can apply this method.

Also if we need to make the button invisible for all users except a user this logic can be applied.

private void FindControlsRecursive(Control root, ref ArrayList list)
{
if (root.Controls.Count != 0)
{
foreach (Control c in root.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.Button"))
{
if (int.Parse(GetSession(GrantSessions.UserID)) != 1)
{
c.Visible = false;
}
list.Add(c);
}
else if (c.HasControls())
FindControlsRecursive(c, ref list);
}
}
}

Comments or Responses

Login to post response