Get All Web Controls of a Specific Type on a Page

后端 未结 8 1874
忘掉有多难
忘掉有多难 2020-12-01 03:58

I have been pondering how I can get all controls on a page and then perform a task on them in this related question:

How to Search Through a C# DropDownList Programm

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 04:02

    You can use recursive logic to get all of the controls, like this:

    private void PopulateSelectList(Control parentCtrl, List selectList)
    {
        foreach (Control ctrl in parentCtrl.Controls)
        {
            if (ctrl is DropDownList)
            {
                selectList.Add(((DropDownList)ctrl);
                continue;
            }
            FindAllControls(ctrl, selectList);
        }
    }
    

提交回复
热议问题