Get All Web Controls of a Specific Type on a Page

2019-01-10 17:40发布

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 Programmatically

I need code that can scan the page, get all DropDownList Controls and return them in a list.

I'm currently having to edit each individual control, I would rather be able to dynamically loop over each control to preform my task.

8条回答
欢心
2楼-- · 2019-01-10 17:44
foreach (DropDownList dr in this.Page.Form.Controls.OfType<DropDownList>())
{

}
查看更多
在下西门庆
3楼-- · 2019-01-10 17:56

Here is a recursive version that returns a control collection of the requested type instead of utilizing another argument:

using System.Collections.Generic;
using System.Web.UI;
// ...
public static List<T> GetControls<T>(ControlCollection Controls)
where T : Control {
  List<T> results = new List<T>();
  foreach (Control c in Controls) {
    if (c is T) results.Add((T)c);
    if (c.HasControls()) results.AddRange(GetControls<T>(c.Controls));
  }
  return results;
}

Insert into your class (static optional).

查看更多
太酷不给撩
4楼-- · 2019-01-10 18:00
        var dropDownLists = new List<DropDownList>();
        foreach (var control in this.Controls)
        {
            if (control is DropDownList)
            {
                dropDownLists.Add( (DropDownList)control );
            }
        }
查看更多
\"骚年 ilove
5楼-- · 2019-01-10 18:02

Looping through controls on a page isn't hard - you just have to look within each control for more controls.

You could do something like

foreach(var control in Page)
{
    if(control is DropDownList)
    {
        //Do whatever
    }
    else
    {
        //Call this function again to search for controls within this control
    }
}
查看更多
姐就是有狂的资本
6楼-- · 2019-01-10 18:02

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

private void PopulateSelectList(Control parentCtrl, List<DropDownList> selectList)
{
    foreach (Control ctrl in parentCtrl.Controls)
    {
        if (ctrl is DropDownList)
        {
            selectList.Add(((DropDownList)ctrl);
            continue;
        }
        FindAllControls(ctrl, selectList);
    }
}
查看更多
欢心
7楼-- · 2019-01-10 18:02

This works if you use the form components from system.web.ui however this does not work when you use them from system.web.mvc apparently so i came up with the following work around.

for (Int32 idx = 0; idx < formCollection.Count; idx += 1)
                    {
                    String Name = formCollection.Keys[idx];
                    String value = formCollection[idx];

                    if (Name.Substring(0, 3).ToLower() == "chk")

                        {
                        Response.Write(Name + " is a checkbox <br/>");
                        }
                    else if (Name.Substring(0, 5).ToLower() == "txtar")
                        {
                        Response.Write(Name + " is a text area <br/>");
                        }
                    else if (Name.Substring(0, 2).ToLower() == "rd")
                        {
                        Response.Write(Name + " is a RadioButton <br/>");
                        }

                    }

This works for me however i found out that radio button if not selected is null so doesnt return anything which is ok i dont have to write anything to the database if it is null

查看更多
登录 后发表回答