简体   繁体   中英

How to loop through all controls in All ASP.NET UpdatePanels in the page?

I am using the following code to loop through my controls. The problem is that I have put all of these controls including TextBox and DropDownList inside AJAX UpdatePanel control and those panels never get reached. I have around three UpdatePanels in the page, so how can I loop through all the controls TextBox and DropDownList inside each UpdatePanel?

This code only works for one UpdatePanel:

foreach (Control c in UpdatePanel2.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }

It should work:

foreach (Control c in Page.Controls)
    {
        if (c is UpdatePanel)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }
    }

To scroll controls within the UpdatePanel do something like this:

For Each ctrl As Control In UpdatePanel1.ContentTemplateContainer.Controls
    If ctrl.GetType() Is GetType(TextBox) Then

    End If
Next

Itiel's answer did not work in my scenario. I have 6 gridviews in an updatepanel. Since it was only one type of control I was looking for inside the UpdatePanel the below routine worked like a charm! My goal was to enable or disable all gridviews on the page with one button click that calls ChangeControlStatus(true or false) method. Maybe it will help someone.

private void ChangeControlStatus(bool status)
    {
        int i = 1;
        //loop through 6 gridviews
        for (i = 1; i <= 6; i++)
        {
            //enable/disable all grids on the page  
            GridView gv = UpdatePanel1.FindControl("UpdatePanel1").FindControl("Gridview" + i) as GridView;
            gv.Enabled = status;
        } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM