Interaction between two listboxes)

Source: Internet
Author: User
Suitable:
. NET 2.0
Vs2005
Effect:

ASPX page:

< Table >
< Tbody >
< Tr >
< TD >
< ASP: ListBox ID = "Lbleft" Runat = "Server" Selectionmode = "Multiple" >
< ASP: listitem > Add name </ ASP: listitem >
< ASP: listitem > Date of birth </ ASP: listitem >
</ ASP: ListBox >
</ TD >
< TD Style = "Width: 27px" >
< ASP: button ID = "Btntoright" Runat = "Server" Text = ">"
Onclick = "Btntoright_click"   />
< BR />
< ASP: button ID = "Btntoleft" Runat = "Server" Text = "<"
Onclick = "Btntoleft_click"   />
</ TD >
< TD Style = "Width: 3px" >
< ASP: ListBox ID = "Lbright" Runat = "Server"
Selectionmode = "Multiple" > </ ASP: ListBox > </ TD >
</ Tr >
</ Tbody >
</ Table >
< ASP: Label ID = "Lblmsg" Runat = "Server" > </ ASP: Label >

Code As follows: 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 );
}
}
}
}

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.