Example of adding, removing, and sorting two listboxes or DropdownList

Source: Internet
Author: User

Directly copy the code and run it directly:

ASPX code
<% @ Page Language = "C #" EnableEventValidation = "false" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">

Protected void button#click (object sender, EventArgs e)
{
Response. Write ("<li> s1 =" + Request. Form [s1.UniqueID]);
Response. Write ("<li> s2 =" + Request. Form [s2.UniqueID]);
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript">
Var bak = null;
Function setData (){
Ss1 = document. getElementById ("<% = s1.ClientID %> ");
Ss2 = document. getElementById ("<% = s2.ClientID %> ");
If (bak = null ){
Bak = [];
For (I = 0; I <ss1.length; I ++) bak. push (ss1 [I]. value );
}

For (I = ss1.length-1; I>-1; I --){
If (ss1 [I]. selected ){
Ss2.options [ss2.options. length] = new Option (ss1 [I]. value, ss1 [I]. value );
Ss1 [I]. parentNode. removeChild (ss1 [I]);
}
}
SortSelect (ss1 );
SortSelect (ss2 );
}

Function removeData (){
Ss1 = document. getElementById ("<% = s1.ClientID %> ");
Ss2 = document. getElementById ("<% = s2.ClientID %> ");
For (I = ss2.length-1; I>-1; I --){
If (ss2 [I]. selected ){
Ss1.options [ss1.options. length] = new Option (ss2 [I]. value, ss2 [I]. value );
Ss2 [I]. parentNode. removeChild (ss2 [I]);
}
}
SortSelect (ss1 );
SortSelect (ss2 );
}

Function SortSelect (ss ){
Var tt = [];
For (I = 0; I <bak. length; I ++ ){
For (j = 0; j <ss. length; j ++ ){
If (ss [j]. value = bak [I]) tt. push (bak [I]);
}
}
Ss. length = 0;
For (I = 0; I <tt. length; I ++ ){
Ss. options [ss. options. length] = new Option (tt [I], tt [I]);
}
}
</Script>
</Head>
<Body>
<Form runat = "server" id = "form1">
<Table>
<Tr>
<Td>
<Asp: ListBox ID = "s1" runat = "server" SelectionMode = "Multiple" Rows = "6">
<Asp: ListItem> A </asp: ListItem>
<Asp: ListItem> B </asp: ListItem>
<Asp: ListItem> C </asp: ListItem>
<Asp: ListItem> D </asp: ListItem>
</Asp: ListBox>
</Td>
<Td>
<Input onclick = "setData ()" type = "button" value = "->"/>
<Br/>
<Input onclick = "removeData ()" type = "button" value = "<-"/>
</Td>
<Td>
<Asp: ListBox ID = "s2" runat = "server" SelectionMode = "Multiple" Rows = "6"> </asp: ListBox>
</Td>
</Tr>
</Table>
<Asp: Button ID = "Button1" runat = "server" onclick = "button#click" Text = "get selected"/>
</Form>
</Body>
</Html>

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