The ListBox control allows a user to select one or more items from a predefined list. It differs from the DropDownList control in that it not only displays multiple items at once, but optionally allows the user to select more than one item at a time.
One, property
| Property |
value |
function |
| SelectionMode |
single| Multiple |
List selection mode: Radio | multiple selection |
| Selected |
false| True |
is a selected state |
Second, the example
Listbox.aspx
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "ListBox.aspx.cs" inherits= "Webcontrols_listbox"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<style type= "Text/css" >
. style1
{
width:293px;
}
. style2
{
width:233px;
}
</style>
<body>
<form id= "Form1" runat= "Server" >
<div>
Province: <asp:listbox id= "Lstprov" runat= "Server" >
<asp:ListItem> Shandong </asp:ListItem>
<asp:listitem selected= "True" > Hebei </asp:ListItem>
<asp:ListItem> Inner Mongolia </asp:ListItem>
<asp:ListItem> Anhui </asp:ListItem>
</asp:ListBox>
<br/>
<asp:button id= "btnsubmit" runat= "Server" text= "Submit" onclick= "btnSubmit_Click"/>
Your choice is: <asp:label id= "lblstate" runat= "Server" ></asp:Label>
2, multiple-selection list box Province: <asp:listbox id= "LSTPROV2" runat= "Server" selectionmode= "multiple" >
<asp:ListItem> Shandong </asp:ListItem>
<asp:listitem selected= "True" > Hebei </asp:ListItem>
<asp:ListItem> Inner Mongolia </asp:ListItem>
<asp:ListItem> Anhui </asp:ListItem>
</asp:ListBox>
Tip: Ctrl + click <br/>
<asp:button id= "BtnSubmit2" runat= "Server" text= "Submit"
onclick= "Btnsubmit2_click"/>
Your choice is: <asp:label id= "LblState2" runat= "Server" ></asp:Label>
</div>
</form>
</body>
ListBox.aspx.cs
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class WebControls_ListBox:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void btnSubmit_Click (object sender, EventArgs e)
{
Lblstate.text = Lstprov.selectedvalue;
}
protected void Btnsubmit2_click (object sender, EventArgs e)
{
String str = string. Empty;
for (int i = 0; i < LstProv2.Items.Count; i++)
{
if (lstprov2.items[i). Selected = = True)
{
str = string. Format ("{0}{1},", Str,lstprov2.items[i]. Value);
}
}
str = str. Substring (0, str. LENGTH-1); Remove the last ","
Lblstate2.text = str;
}
}