跳到主要內容

ListBox 2 ListBox (Web Form)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBoxTest.aspx.cs" Inherits="VS2013Test.RightMgm.ListBoxTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table align="center">
                <tr>
                    <td align="center" colspan="3">
                        <b>ListBox Sample</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:ListBox ID="ListBox1" runat="server" Height="169px" Width="121px" SelectionMode="Multiple">
                        </asp:ListBox>
                    </td>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <asp:Button ID="btn1" runat="server" Text=">" Width="45px" OnClick="btn1_Click" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="btn2" runat="server" Text=">>" Width="45px" OnClick="btn2_Click" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="btn3" runat="server" Text="<" Width="45px" OnClick="btn3_Click" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="btn4" runat="server" Text="<<" Width="45px" OnClick="btn4_Click" />
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <asp:ListBox ID="ListBox2" runat="server" Height="169px" Width="121px" SelectionMode="Multiple">
                            <asp:ListItem Value="0">SQl Server</asp:ListItem>
                            <asp:ListItem Value="1">Sharesoint</asp:ListItem>
                            <asp:ListItem Value="2">Jquery</asp:ListItem>
                        </asp:ListBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="GetValue" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

-----------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace VS2013Test.RightMgm
{
  
    public partial class ListBoxTest : System.Web.UI.Page
    {
        ArrayList arraylist1 = new ArrayList();
        ArrayList arraylist2 = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
           if(!Page.IsPostBack)
           {
               ListBox1.Items.Add(new ListItem("Asp.Net", "0"));
               ListBox1.Items.Add(new ListItem("C#.Net", "1"));
               ListBox1.Items.Add(new ListItem("VB.Net", "2"));
               ListBox1.Items.Add(new ListItem("JavaScript", "3"));
               ListBox1.Items.Insert(0,new ListItem("Ajax", "4"));
           }
         
        }
        /// <summary>
        /// btn1_Click event is used to move single or multiple items from Listbox1 to Listbox2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn1_Click(object sender, EventArgs e)
        {
            lbltxt.Visible = false;
            if (ListBox1.SelectedIndex >= 0)
            {
                for (int i = 0; i < ListBox1.Items.Count; i++)
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        if (!arraylist1.Contains(ListBox1.Items[i]))
                        {
                            arraylist1.Add(ListBox1.Items[i]);
                        }
                    }
                }
                for (int i = 0; i < arraylist1.Count; i++)
                {
                    if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
                    {
                        ListBox2.Items.Add(((ListItem)arraylist1[i]));
                    }
                    ListBox1.Items.Remove(((ListItem)arraylist1[i]));
                }
                ListBox2.SelectedIndex = -1;
            }
            else
            {
                lbltxt.Visible = true;
                lbltxt.Text = "Please select atleast one in Listbox1 to move";
            }
        }
        /// <summary>
        /// btn2_Click event is used to move all items from Listbox1 to Listbox2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn2_Click(object sender, EventArgs e)
        {
            lbltxt.Visible = false;
            while (ListBox1.Items.Count != 0)
            {
                for (int i = 0; i < ListBox1.Items.Count; i++)
                {
                    ListBox2.Items.Add(ListBox1.Items[i]);
                    ListBox1.Items.Remove(ListBox1.Items[i]);
                }
            }
        }
        /// <summary>
        /// btn3_Click event is used to move single or multiple items from Listbox2 to Listbox1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn3_Click(object sender, EventArgs e)
        {
            lbltxt.Visible = false;
            if (ListBox2.SelectedIndex >= 0)
            {
                for (int i = 0; i < ListBox2.Items.Count; i++)
                {
                    if (ListBox2.Items[i].Selected)
                    {
                        if (!arraylist2.Contains(ListBox2.Items[i]))
                        {
                            arraylist2.Add(ListBox2.Items[i]);
                        }
                    }
                }
                for (int i = 0; i < arraylist2.Count; i++)
                {
                    if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
                    {
                        ListBox1.Items.Add(((ListItem)arraylist2[i]));
                    }
                    ListBox2.Items.Remove(((ListItem)arraylist2[i]));
                }
                ListBox1.SelectedIndex = -1;
            }
            else
            {
                lbltxt.Visible = true;
                lbltxt.Text = "Please select atleast one in Listbox2 to move";
            }
        }
        /// <summary>
        /// btn4_Click event is used to move all items form Listbox2 to Listbox1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn4_Click(object sender, EventArgs e)
        {
            lbltxt.Visible = false;
            while (ListBox2.Items.Count != 0)
            {
                for (int i = 0; i < ListBox2.Items.Count; i++)
                {
                    ListBox1.Items.Add(ListBox2.Items[i]);
                    ListBox2.Items.Remove(ListBox2.Items[i]);
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if(ListBox2.Items.Count < 0)
            {
                return;
            }
            string selectValue = "";
            foreach (ListItem item in ListBox2.Items)
            {
                selectValue += item.Text + "-" + item.Value + "/";
            }
            lbltxt.Visible = true;
            lbltxt.Text = selectValue;
        }

    }
    }