asp.net下拉列表

时间:2009-09-04 10:32:41

标签: c# asp.net

我使用addrange方法将项目添加到dropdownlist,这是我的代码

ListItem[] cou =
new ListItem[8]{"India",
                "United States",
                "United Kingdom",
                "Canada",
                "Singapore",
                "Australia",
                "Sudia Arabia",
                "South Africa" };
dpcountry.Items.AddRange(cou);

但它给了我错误,因为无法隐式转换字符串ti listitem

请给我一个解决方案

提前谢谢 桑吉塔

4 个答案:

答案 0 :(得分:3)

您需要创建新的ListItems

string[] cou =new string[8]{
              "India",
              "United States",
              "United Kingdom",
              "Canada",
              "Singapore",
              "Australia",
              "Sudia Arabia",
              "South Africa" };
dpcountry.Items.AddRange(cou.Select(c => new ListItem(c));

您还需要对System.Linq的引用,

善,

答案 1 :(得分:2)

我试过Dan的例子,但是必须添加.ToArray()才能让它工作,即:

string[] cou =new string[8]{
              "India",
              "United States",
              "United Kingdom",
              "Canada",
              "Singapore",
              "Australia",
              "Saudi Arabia",
              "South Africa" };

dpcountry.Items.AddRange(cou.Select(c => new ListItem(c)).ToArray());

答案 2 :(得分:1)

   object []cou = new object[]{"India",
                               "United States",
                               "United Kingdom",
                               "Canada",
                               "Singapore",
                               "Australia",
                               "Sudia Arabia",
                               "South Africa" };
    dpcountry.Items.AddRange(cou);

答案 3 :(得分:1)

您正在创建ListItem类型的数组,但您正在尝试向此数组添加字符串。这就是你得到这个错误的原因。要使此代码生效,您应将其更改为:

new ListItem[8]{ new ListItem("India"), new ListItem("United"), /* etcetera */ };