代码之家  ›  专栏  ›  技术社区  ›  jos

下拉列表列表中的项目

  •  1
  • jos  · 技术社区  · 17 年前

    有谁能告诉我在一个列表中添加所有dropdownlist项目的最短方法吗 List<string>

    列表<字符串>

    5 回复  |  直到 17 年前
        1
  •  8
  •   BigBlondeViking    17 年前

    取决于您是否需要ListItem文本以及是否能够利用3.5

    public static List<string> GetStrings(DropDownList dl)
    {
        return dl.Items.Cast<ListItem>().Select(i => i.Text).ToList();
    }
    
        2
  •  3
  •   kemiller2002    17 年前

    您的帖子有点不清楚您是在下拉列表中添加项目还是在列表中添加项目?

    要添加到列表中,请执行以下操作: var list=新列表(DropDownList.Items.Length);

    foreach(var item in DropDownList.Items.Length)
       list.Add(item.Text);
    

    要添加到下拉列表中,请执行以下操作:

    var list = new List<string> ();
        DropDownList.DataSource = list;
        DropDownList.DataBind();
    
        3
  •  2
  •   Mitch Wheat    17 年前

    是的。将数据源设置为列表<字符串>

        4
  •  1
  •   d4nt    17 年前
    myDropDown.DataSource = myListOfStrings;
    myDropDown.DataBind();
    
        5
  •  0
  •   NikolaiDante    17 年前

    ddl你的下拉列表。

    这应该起作用:

    foreach (var str in ListOfStrings)
    {
      ddl.Items.Add(new ListItem(str, str);
    }
    
    推荐文章