简体   繁体   中英

Retrieving drop down list items from list of controls

I am attempting to retrieve a ListItemCollection from a List of controls. This List contains many controls of different types - DropDownList , Label , TextBox .

I would like to retrieve all ListItem from all the DropDownList controls contained in the original List of controls.

My thought process so far has been to extract all of the DropDownList controls into a new list, and iterate though this to pull out each ListItem - however, every DropDownList control is coming up with 0 items

ControlCollection cList = pnlContent.Controls;

List<DropDownList> ddlList = new List<DropDownList>();
foreach (Control c in cList)
{
    if (c.GetType() == new DropDownList().GetType())
    {
        ddlList.Add((DropDownList)c);
    }
}

ListItemCollection itemCollection = new ListItemCollection();
foreach (DropDownList ddl in ddlList)
{
    foreach(ListItem li in ddl.Items)
    {
        itemCollection.Add(li);
    }
}

I'm sure this is the wrong (and massively inefficient) way of doing this. Any help would be appreciated.

This will do:

public IEnumerable<ListItem> GetListItems(ControlCollection controls)
{
    return controls.OfType<DropDownList>().SelectMany(c => c.Items.OfType<ListItem>());
}

I currently do not have an installation where I can test this, but as a line of thought I would use Linq to do this.

Here is an example that you should be able to use;

var type = new DropDownList().GetType();

var listOfControl = from c in pnlContent.Controls
                    where c.GetType() == type
                    select ((DropDownList)c).Items;

Try this:

 var ddlList = cList.OfType<DropDownList>();


 ListItemCollection itemCollection = new ListItemCollection();
 // option 1
 var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li).ToArray();
 itemCollection.AddRange(temp);
 // or option 2
 var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li);
 foreach (var listItem in temp)
 {
     itemCollection.Add(listItem);
 }
        comboBox1.Items.Add(button1);
        comboBox1.Items.Add(button2);
        comboBox1.Items.Add(dateTimePicker1);
        comboBox1.Items.Add(checkBox1);

        comboBox2.Items.Add(button3);
        comboBox2.Items.Add(button4);
        comboBox2.Items.Add(dateTimePicker2);
        comboBox2.Items.Add(checkBox2);

        comboBox3.Items.Add(button5);
        comboBox3.Items.Add(dateTimePicker3);
        comboBox3.Items.Add(checkBox3);
        comboBox3.Items.Add(checkBox4);

        List<ComboBox> ddlList = new List<ComboBox>();
        foreach (Control c in panel1.Controls)
        {
            if (c is ComboBox)
            {
                ddlList.Add((ComboBox)c);
            }
        }

        List<Control> itemCollection = new List<Control>();

        foreach (ComboBox ddl in ddlList)
        {
            foreach (var li in ddl.Items)
            {
                itemCollection.Add((Control)li);
            }
        }

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