two ListBox interactions

Source: Internet
Author: User
Interaction between two listboxes

ASPX page:

<Asp: ListBox id = "lbleft" runat = "server" selectionmode = "multiple">
<Asp: listitem> Add a name </ASP: listitem>
<Asp: listitem> Date of Birth </ASP: listitem>
</ASP: ListBox>
<TD style = "width: 27px">
<Asp: button id = "btntoright" runat = "server" text = ">"
Onclick = "btntoright_click"/>
<Asp: button id = "btntoleft" runat = "server" text = "<"
Onclick = "btntoleft_click"/>
<TD style = "width: 3px">
<Asp: ListBox id = "lbright" runat = "server"
Selectionmode = "multiple"> </ASP: ListBox> </TD>
<Asp: Label id = "lblmsg" runat = "server"> </ASP: Label>

CS code: 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;

Public partial class test1: system. Web. UI. Page
Protected void page_load (Object sender, eventargs E)

Protected void btntoright_click (Object sender, eventargs E)
If (lbleft. selecteditem! = NULL)
Additemfromsourcelistbox (lbleft, lbright );

Removeselecteditem (lbleft );

Lblmsg. Text = ""; // Note: Why is this line required?

Foreach (listitem item in lbright. Items)
If (item. Selected)
Lblmsg. Text + = item. text;

Protected void btntoleft_click (Object sender, eventargs E)
If (lbright. selecteditem! = NULL)
Additemfromsourcelistbox (lbright, lbleft );
Removeselecteditem (lbright );

Private void removeselecteditem (ListBox listcontrol)
While (listcontrol. selectedindex! =-1)
Listcontrol. Items. removeat (listcontrol. selectedindex );

Private void additemfromsourcelistbox (ListBox sourcebox, ListBox targetbox)
Foreach (listitem item in sourcebox. Items)
If (item. Selected = true &&! Targetbox. Items. Contains (item ))
Targetbox. Items. Add (item );


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: 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.