Loop Through All Controls on ASP.NET Webpage

Loop through all controls on asp.net webpage

Your original method will not work if you start from the root element of your document: something like page.Controls as you will only loop through the first level of controls, but remember a control can be composite. So you need recursion to pull that off.

        public void FindTheControls(List<Control> foundSofar, Control parent) 
{

foreach(var c in parent.Controls)
{
if(c is IControl) //Or whatever that is you checking for
{

foundSofar.Add(c);

if(c.Controls.Count > 0)
{
this.FindTheControls(foundSofar, c);
}
}

}

}

Loop through controls asp.net

What you want to do is disable/enable controls that are children of the page, and also controls that are children of controls that are children of the page.
For this you will need to use a recursive function.
Something like:

private void DisableChildControls(ControlCollection controls, int depthLimit)
{
if(depthLimit <= 0)
return;
foreach(var ctl in controls)
{
ctl.Enabled = false;
if(ctl.Controls.Count > 0)
{
DisableChildControls(ctl.Controls, --depthLimit);
}
}
}

In the page load event you call start the traversal using:

if(this.Controls.Count > 0)
DisableChildControls(this.Controls, 2); //If you want the depth to be two levels.

This will recursivly disable controls down the tree, up to the limit you specified.
Just consider that for a complex page, this recursive operation can take a considerable amount of time.

Also note that for better or worse, this will only loop through controls marked with runat="server"

How to iterate over the controls inside an asp.net page?

You either have to use a recursive function and pass a control as a parameter to that function, the function will then loop through the controls inside that control, see Loop through all controls on asp.net webpage

Or you have to put your listboxes and textboxes inside a panel and loop through the controls inside that panel, something like this:

<asp:Panel ID="pnl" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Panel>

<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />

And the code behind:

protected void btnClear_Click(object sender, EventArgs e)
{
ClearForms(pnl);
}

public void ClearForms(Control c)
{
foreach (var item in c.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = String.Empty;
}
if (item is ListItem)
{
((ListItem)item).Text = "Choose...";
((ListItem)item).Value = "-1";
}
}
}

Explanation to your case: the control.Controls only fetches the first level controls inside the control, so when you call Page.Controls, it only fetches whatever server controls are there on your page's first level of elements.

hope this helps...

Loop Through Controls on Web User Control

I finally figured out what is going on.

I have the checkboxes inside different tables. These tables contains runat="server". This table is inside a Div tag that also contains a runat="server".

My code could never find the checkboxes because of this. I had to add a For Each that loops through the Div tag and find the appropriate table(s). I then had to loop through the tables in order to find the checkboxes.

ASP.Net / C#, Loop through certain controls on a page?

This cannot be done entirely using LINQ but you could have an extension defined like this

static class ControlExtension
{
public static IEnumerable<Control> GetAllControls(this Control parent)
{
foreach (Control control in parent.Controls)
{
yield return control;
foreach (Control descendant in control.GetAllControls())
{
yield return descendant;
}
}
}
}

and call

this.GetAllControls().OfType<TextBox>().ToList().ForEach(t => t.Enabled = false);

Loop Through Controls on Web User Control

I finally figured out what is going on.

I have the checkboxes inside different tables. These tables contains runat="server". This table is inside a Div tag that also contains a runat="server".

My code could never find the checkboxes because of this. I had to add a For Each that loops through the Div tag and find the appropriate table(s). I then had to loop through the tables in order to find the checkboxes.



Related Topics



Leave a reply



Submit