Loop through all controls on asp.net webpage

后端 未结 7 1435
攒了一身酷
攒了一身酷 2020-12-01 22:17

I need to loop through all the controls in my asp.net webpage and do something to the control. In one instance I\'m making a giant string out of the page and emailing it to

7条回答
  •  鱼传尺愫
    2020-12-01 23:03

    Even though this question has been discussed for more than 9 years, here I leave the code that worked for me based on @Jagadheesh Venkatesan's code.

    private List getControls()
    {
        List lControls = new List();
        foreach (Control oControl in Page.Controls)
            foreach (Control childControl1 in oControl.Controls)
                foreach (Control childControl2 in childControl1.Controls)
                    foreach (Control childControl3 in childControl2.Controls)
                        if (childControl3.GetType().ToString().Contains("System.Web.UI.WebControls"))
                            lControls.Add(childControl3);
        return lControls;
    }
    

提交回复
热议问题