c#dropdownlist避免多个insert语句?

时间:2017-05-29 07:07:31

标签: c# asp.net webforms

这是c#代码:

DropDownList ddl;
ddl.Items.Insert(0, new ListItem("--Select Me--", "0"));
ddl.Items.Insert(1, new ListItem("I'm the first", "1"));
ddl.Items.Insert(2, new ListItem("I'm the second", "2"));
ddl.Items.Insert(3, new ListItem("I'm the third", "3"));

有没有办法可以避免这些顺序插入下拉列表并批量插入这些值?

5 个答案:

答案 0 :(得分:1)

你可以这样做:

DropDownList ddl;
var ls=new List<KeyValuePair<int,string>>()
{
    new KeyValuePair<int,string>(0,"--Select Me--"),
    new KeyValuePair<int,string>(1,"I'm the first"),
    new KeyValuePair<int,string>(2,"I'm the second"),
    new KeyValuePair<int,string>(3,"I'm the third"),
};
ddl.DataTextField="Value";
ddl.DataValueField="Key";
ddl.DataSource=ls;
ddl.DataBind();

答案 1 :(得分:1)

使用Add a thousands separator to a total with Javascript or jQuery? 方法:

DropDownList ddl;
ddl.Items.AddRange(new ListItem[] { new ListItem ("--Select Me--", "0"), new ListItem("I'm the first", "1"), new ListItem("I'm the second", "2"), new ListItem("I'm the third", "3")});

答案 2 :(得分:1)

这可能会有所帮助:

List<ListItem> items = new List<ListItem>();
  items.Add(new ListItem("Alabama", "Alabama"));
  items.Add(new ListItem("Alaska", "Alaska"));
  DropDownList1.Items.AddRange(items.ToArray());

答案 3 :(得分:1)

document.querySelector('.myclass').dataset.url = data.results[0].alias;

答案 4 :(得分:0)

试试这个

ddl.Items.Add(new ListItem("--Select Me--", "0"));
ddl.Items.Add(new ListItem("I'm the first", "1"));
ddl.Items.Add(new ListItem("I'm the second", "2"));
ddl.Items.Add(new ListItem("I'm the third", "3"));