asp.net the listbox binding multiple options to select and delete implementation methods _ Practical Tips

Source: Internet
Author: User
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.
Related Article

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.