In asp.net, multiple options of ListBox binding are selected and deleted.

Source: Internet
Author: User

Let's take a look at the implementation of listbox binding with multiple options. Copy codeThe Code is 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>
<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 options, but how can I bind multiple options while selecting multiple options.

. Aspx:Copy codeThe Code is as follows: <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:Copy codeThe Code is 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;
}

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:Copy codeThe Code is as follows: 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 optionsCopy codeThe Code is as follows: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.