ListBox basic function (ListBox1---listbox exchange)

Source: Internet
Author: User

Although there have been similar functions before: But there are some problems, this time rewrite it.

The ListBox basic function is to add the list item first, and the client implementation code is added in the middle of the ListBox instantiation code, for example:
<asp:listitem value= "Value" selected=true>text</asp:listitem>

If implemented on the server side, to avoid adding list items each time the load is loaded, the code above is included in the following code:
if (! IsPostBack)
{
}
WebForm page must add 2 ListBox (ListBox1 and Lixtbox2) and 2 command buttons, listbox1 is not empty. List items are added from ListBox1 to listbox2 the Add method must be called in the Button1 Click event:
LISTBOX2.ITEMS.ADD (Listbox1.selectedvalue);

To remove a list item from ListBox2, you must call the Remove method in the Button2 click event:
ListBox2.Items.Remove (Listbox2.selectedvalue);

List items are removed from the listbox1 after they have been added to the ListBox2 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;
}

This can only achieve a single add, want to implement a number of add, first set ListBox1 SelectionMode property value Multiple,listbox1 allow multiple items selected.

This can only achieve a single add, want to implement a number of add, first set ListBox1 SelectionMode property value Multiple,listbox1 allow multiple items selected.

Add in Button1 Click event
foreach (ListItem myitem in Listbox1.items)
if (myitem.selected==true)
LISTBOX2.ITEMS.ADD (myitem);

You want to empty ListBox2 all the options in the Button2 Click event Call the Clear method.
ListBox2.Items.Clear ();

If the list item has been added and is not allowed to be added two times, the code in the Button1 click event is included in:
if (ListBox2.Items.FindByValue (listbox1.selectedvalue) ==null)
{
}


The listbox is bound to the database to specify his datasource and DataTextField attributes,
listbox2.datasource= data source;
listbox2.datatextfield= "field name";
Listbox2.databind ();

--------------------------------------------------------------------------------------------------------------- -------

My approach:

Controls ListBox1 and LISTBOX2,BTN1 (>), BTN2 (>>), Btn3 (<), Btn4 (<<)

The ListBox state control is associated with a DDL.

Multiple selection:

ListBox1 and ListBox2 set SelectionMode property values multiple

Background practice:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Text;

public partial class Eflow_WkHourGrpAdd:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{

}

}
protected void But1_click (object sender, EventArgs e)
{
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;
}
/*
if (ListBox2.Items.FindByValue (listbox1.selectedvalue) = null)
{
DataAccess.MessageObject.Show (ListBox1.SelectedValue.ToString ());
}
*/
/*
foreach (ListItem myitem in Listbox1.items)
if (myitem.selected = = True)
LISTBOX2.ITEMS.ADD (myitem);
*/
}
protected void But2_click (object sender, EventArgs e)
{
Listbox1.databind ();
Listbox2.datasource = Listbox1.items;
Listbox2.databind ();
ListBox1.Items.Clear ();
}
protected void But3_click (object sender, EventArgs e)
{
int i = 0;
while (I < ListBox2.Items.Count)
{
if (listbox2.items[i). Selected = = True)
{
LISTBOX1.ITEMS.ADD (Listbox2.items[i]);
ListBox2.Items.Remove (Listbox2.items[i]);
}
Else
i + 1;
}
}
protected void But4_click (object sender, EventArgs e)
{
ListBox2.Items.Clear ();
Listbox1.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.