Loop through all the user controls on a page

后端 未结 4 1157
小蘑菇
小蘑菇 2020-12-19 12:34

Want to loop through all the user controls that exist on the page and get their IDs. How do I do it?

4条回答
  •  -上瘾入骨i
    2020-12-19 13:09

    To get each User Control, you'd have to test the Type of the control:

    EDIT: I modified my example to go through all controls recursively:

    Method

    public void GetUserControls(ControlCollection controls)
    {
        foreach (Control ctl in controls)
        {
            if (ctl is UserControl)
            {
                // Do whatever.
            }
    
            if (ctl.Controls.Count > 0)
                GetUserControls(ctl.Controls);
        }
    }
    

    Called

    GetUserControls(Page.Controls);
    

提交回复
热议问题