Basic functions of the ListBox control

Source: Internet
Author: User
The basic function of ListBox is to add a list item first. The client-side implementation code is added in the middle of the listbox instantiation code, for example:
<Asp: ListItem Value = "value" Selected = True> Text </asp: ListItem>
If it is implemented on the server side, to avoid adding list items during each load, the above code is included in the following code:
If (! IsPostBack)
{
}
Two listbox (listbox1 and lixtbox2) and two command buttons must be added to the WebForm page. listbox1 is not empty. To Add a list item from listbox1 to listbox2, you must call the Add method in the Button1 click event:
ListBox2.Items. Add (ListBox1.SelectedValue );
To delete a list item from listbox2, you must call the Remove method in the Button2 click event:
ListBox2.Items. Remove (ListBox2.SelectedValue );
After the list item is added from listbox1 to listbox2, the list item is deleted from listbox1:
Int I = 0;
While (I <ListBox1.Items. Count)
{
If (ListBox1.Items [I]. Selected = true)
{
ListBox2.Items. Add (ListBox1.Items [I]);
ListBox1.Items. Remove (ListBox1.Items [I]);
}
Else
I + = 1;
}
In this way, only one item can be added. To add Multiple items, first set the value of the SelectionMode attribute of ListBox1 to Multiple. ListBox1 allows Multiple items to be selected.
In Button1, click the event to add
Foreach (ListItem MyItem in ListBox1.Items)
If (MyItem. Selected = true)
ListBox2.Items. Add (MyItem );
To clear all options in ListBox2 at a time, call the clear method in the Button2 click event,
ListBox2.Items. Clear ();
If the list item has been added and cannot be added again, the code in the Button1 click event is included in:
If (ListBox2.Items. FindByValue (ListBox1.SelectedValue) = null)
{
}
When ListBox is bound to a database, its DataSource and DataTextField attributes are specified,
ListBox2.DataSource = data source;
ListBox2.DataTextField = "field name ";
ListBox2.DataBind ();

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.