Let's take a look.Listbox bind multiple options
<% @ 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>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<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>
</Html>
The above is only bound to multiple, but what should I do?Bind multiple options and select multiple optionsAnd.
. Aspx:
| The code is as follows: |
Copy code |
<Asp: TextBox ID = "TextBox1" runat = "server" Width = "300"> </asp: TextBox>
<Br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Binding" OnClick = "button#click"/>
<Br/>
<Br/>
<Asp: ListBox ID = "ListBox1" runat = "server" Height = "100" SelectionMode = "Multiple"> </asp: ListBox>. |
In aspx. cs, the first step is to prepare data for ListBox, and then bind the data to the ListBox control:
| The code is as follows: |
Copy code |
|
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.111cn.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.111cn.net );
Site. Add ("163", "http://www.163.com ");
Site. Add ("QQ", "http://www.qq.com ");
Return site;
} |
To split the TextBox string into multiple values by ";", the namespace is referenced.
Using System. Collections; the next step is to write the click event of the button. The code is quite simple. Insus. NET does not comment much here:
| The code is as follows: |
Copy code |
Protected void button#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 delete multiple options of listbox.
Delete multiple options
| The code is as follows: |
Copy code |
|
Int coun = listBox2.SelectedItems. Count;
For (; coun> = 1; coun --)
{
ListBox2.Items. RemoveAt (listBox2.SelectedIndices [coun-1]);
}
ListBox2.ClearSelected ();
} |
Reply from a netizen
Foreach (object d in listBox2.SelectedItems)
There is a problem with this line. You know that when you delete one of the options, the options of listBOx have been changed, and you can call foreach again. Of course there will be a problem!
The solution is to put listBox2.SelectedItems into an array variable and then call it!
However, the simpler method is to directly call
ListBox2.ClearSelected ();
Yes, this statement solves all the problems! All selected projects will be cleared.
Summary
This article describes how to delete multiple bound option methods in listbox by binding multiple options to listbox and setting multiple selected options at the same time.