12.14 webform 列表框吧 ListBox 列表框

ListBox 列表框

autopostback:true:

if(!ispostback)
{
}

1:绑定数据:
if(!IsPostBack)
{
NationDataContext context = new NationDataContext();
ListBox1.DataSource = context.Nation;
ListBox1.DataTextField = "Name";
ListBox1.DataValueField = "Code";
ListBox1.DataBind();
}}


2:获取选中项的值
单选:Label1.Text= ListBox1.SelectedValue.ToString();
多选:属性>selectionmode>mutiple可单选

protected void Button1_Click(object sender, EventArgs e)
{

foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
Label1.Text +=item.Value;
}

}
3:设置选中项

protected void Button2_Click(object sender, EventArgs e)
{

foreach (ListItem item in ListBox1.Items)
{
if (item.Text == "汉族" || item.Text == "满族")
{
item.Selected = true;

}

}

}

 

posted @ 2015-12-15 14:34  小热包1  阅读(299)  评论(0编辑  收藏  举报