ListBox-->SelectionMode属性:列表的选择模式
在工具箱拖放一个ListBox:
打开源:
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtAddListValue" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="新增" onclick="btnSubmit_Click" />
<br /><br />
<asp:ListBox ID="listValue" runat="server" SelectionMode="Multiple">
<asp:ListItem>张三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
<asp:ListItem>王五</asp:ListItem>
<asp:ListItem>赵六</asp:ListItem>
</asp:ListBox>
<br /><br />
<asp:Button ID="btnSure" runat="server" Text="选择" onclick="btnSure_Click" />
<br />
<asp:Label ID="labShow" runat="server"></asp:Label>
</div>
</form>
</body>
双击新增按钮:
/// <summary>
/// 为ListBox新加一项值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSubmit_Click(object sender, EventArgs e)
{
listValue.Items.Add(txtAddListValue.Text.ToString());
}
双击选择按钮:
/// <summary>
/// 显示选择的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSure_Click(object sender, EventArgs e)
{
labShow.Text = "您选择的是:<br />";
//遍历控件
foreach(ListItem li in listValue.Items)
{
//获取值
if(li.Selected)
{
labShow.Text += li.Text + "<br>";
}
}
}