webform下拉列表、列表框

下拉列表:DropDownList
1.绑定数据:
DropDownList1.DataSource = context.Nation;
DropDownList1.DataValueField = "Code";
DropDownList1.DataTextField = "Name";

DropDownList1.DataBind();

2.取选中项的值
DropDownList1.SelectedValue.ToString();

3.设置哪一项选中
DropDownList1.SelectedIndex = 2; //方式1
foreach (ListItem item in DropDownList1.Items)//方式二
{
if (item.Value == "n002")
{
item.Selected = true;
}
}

ListBox:列表框
1.绑定数据:
ListBox1.DataSource = context.Nation;
ListBox1.DataTextField = "Name";
ListBox1.DataValueField = "Code";

ListBox1.DataBind();
2.取选中项的值:
单选:ListBox1.SelectedValue.ToString();
多选:
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
Label1.Text += item.Value;
}
}
3.设置哪项被选中
foreach (ListItem item in ListBox1.Items)
{
if (item.Text=="汉族" || item.Text=="满族")
{
item.Selected = true;
}
}

posted @ 2015-12-16 18:19  软件工程开发  阅读(2057)  评论(0)    收藏  举报