Let's take a look at the ListBox binding multi-option implementation
Copy Code code as follows:
<%@ Page language= "C #"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "Server" >
protected void Page_Load (object sender, EventArgs e)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (listbox1.items[i). Value = = "A" | | Listbox1.items[i]. Value = = "C")
{
Listbox1.items[i]. Selected = true;
}
}
}
</script>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<asp:listbox id= "ListBox1" runat= "Server" selectionmode= "multiple" >
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
<asp:ListItem>E</asp:ListItem>
</asp:ListBox>
</form>
</body>
It's just a number of bindings, but how do I bind multiple options and select more than one option?
. aspx:
Copy Code code as follows:
<asp:textbox id= "TextBox1" runat= "Server" width= "></asp:TextBox>"
<br/>
<asp:button id= "Button1" runat= "Server" text= "Binding" onclick= "Button1_Click"/>
<br/>
<br/>
<asp:listbox id= "ListBox1" runat= "height=" selectionmode= "multiple" ></asp:ListBox>.
In Aspx.cs, the first is to prepare the data for the listbox and then data bind to the ListBox control:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Data_binding ();
}
}
private void Data_binding ()
{
This. Listbox1.datasource = Site ();
This. Listbox1.datatextfield = "key";
This. Listbox1.datavaluefield = "value";
This. Listbox1.databind ();
}
Private dictionary<string, string> Site ()
{
dictionary<string, string> site = new dictionary<string, string> ();
Site. ADD ("Insus.net cnblogs", http://www.jb51.net);
Site. ADD ("Microsoft", "http://www.microsoft.com");
Site. ADD ("Google", "http://www.google.com");
Site. ADD ("Yahoo", "http://www.yahoo.com.cn");
Site. ADD ("Ifeng", "http://www.ifeng.com");
Site. ADD ("Sina", http://www.baidu.com);
Site. ADD ("163", "http://www.163.com");
Site. ADD ("QQ", "http://www.qq.com");
return site;
}
In order for the string of the textbox to be ";" Split into multiple values, referencing namespaces
Using System.Collections; Next, is writes the button the Click event, the code is quite simple, insus.net does not make the excessive annotation here:
Copy Code code as follows:
protected void Button1_Click (object sender, EventArgs e)
{
String[] s = this. TextBox1.Text.Split (';');
foreach (ListItem li in this.) Listbox1.items)
{
Li. Selected = ((IList) s). Contains (li. Text)? True:false;
}
}
Finally, let's take a look at how to remove multiple options for a ListBox
Remove multiple options
Copy Code code as follows:
int Coun =listbox2.selecteditems.count;
for (;coun> =1;coun--)
{
ListBox2.Items.RemoveAt (listbox2.selectedindices[coun-1]);
}
Listbox2.clearselected ();
}
A netizen reply
foreach (object D in Listbox2.selecteditems)
There is a problem with this line, you know, when you delete one of the options, the ListBox's options have been changed, you call foreach, of course, there will be a problem!
The workaround is to put the Listbox2.selecteditems in an array variable and then call it.
But of course the simpler way is to call directly
Listbox2.clearselected ();
Yes, this one sentence solves all the problems! All selected items will be wiped out.
Summary
This article describes the option for a listbox to set multiple selections while binding multiple options and a ListBox, and finally how to remove multiple-bound option methods.