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;
}
}
}