Suitable:
. NET 2.0
Vs2005
Effect:
ASPX page:
<Table>
<Tbody>
<Tr>
<TD>
<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>
<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>
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 );
}
}
}
}