2013年9月10日 星期二

[轉貼] listbox多選框實現多選數據選擇、上移、下移、左移、右移

出處:http://www.sosuo8.com/article/show.asp?id=2921

效果正如下所示,最近發現後台需要大量使用到,在幾個同事做的基礎上進行了完善,感謝我那幾個可愛的同事。為了實現無刷,采用.NET 3.5的AJAX框架,詳細代碼可以看下面:


前台代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>listbox多選框實現多選數據選擇、上移、下移、左移、右移</title>
</head>
<body> 
    <form id="form1" runat="server"> 
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
            <ContentTemplate> 
                <table> 
                    <tr> 
                        <td><asp:ListBox ID="lbxSource" runat="server" Rows="10" Width="186px" SelectionMode="Multiple"></asp:ListBox></td> 
                        <td> 
                             <asp:Button ID="btnToRight" runat="server" Text=">"  Width="30px" CommandName="ToRight" OnCommand="listBoxOperate_OnCommand" /><br /> 
                             <asp:Button ID="btnAllToRight" runat="server" Text=">>" CommandName="AllToRight" OnCommand="listBoxOperate_OnCommand" Width="30px" /><br /> 
                             <asp:Button ID="btnDel" runat="server" Text="<" CommandName="ToLeft" OnCommand="listBoxOperate_OnCommand" Width="30px" /><br /> 
                             <asp:Button ID="btnAllDel" runat="server" Text="<<" CommandName="AllToLeft" OnCommand="listBoxOperate_OnCommand" Width="30px" /> 
                        </td> 
                        <td><asp:ListBox ID="lbxTo" runat="server" Rows="10" Width="186px" SelectionMode="Multiple"></asp:ListBox></td> 
                        <td> 
                            <asp:Button ID="btnUp" runat="server" Text="↑"  Width="30px" CommandName="ToUp" OnCommand="listBoxOperate_OnCommand" /><br /> 
                            <asp:Button ID="btnDown" runat="server" Text="↓"  Width="30px" CommandName="ToDown" OnCommand="listBoxOperate_OnCommand" /> 
                        </td> 
                    </tr> 
                </table> 
            </ContentTemplate> 
        </asp:UpdatePanel> 
    </form> 
</body> 
</html>
後台代碼:       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                for (int i = 0; i < 10; i++)
                {
                    this.lbxSource.Items.Add(new ListItem("★" + i, i.ToString()));
                }
            }
        }
        protected void listBoxOperate_OnCommand(object sender, CommandEventArgs e)
        {
            ArrayList arrTo = new ArrayList();
            switch (e.CommandName)
            {
                case "ToRight":
                    if (this.lbxSource.SelectedIndex == -1)
                    {
                        break;
                    }
                    else
                    {
                        foreach (ListItem item in lbxSource.Items)
                        {
                            if (item.Selected)
                            {
                                arrTo.Add(item);
                            }
                        }
                        foreach (ListItem item in arrTo)
                        {
                            this.lbxTo.Items.Add(item);
                            this.lbxSource.Items.Remove(item);
                        }
                        break;
                    }
                case "AllToRight":
                    if (this.lbxSource.Items.Count > 0)
                    {
                        foreach (ListItem item in lbxSource.Items)
                        {
                            this.lbxTo.Items.Add(item);
                        }
                        this.lbxSource.Items.Clear();
                        break;
                    }
                    else
                    {
                        break;
                    }
                case "ToLeft":
                    if (this.lbxTo.SelectedIndex == -1)
                    {
                        break;
                    }
                    else
                    {
                        foreach (ListItem item in lbxTo.Items)
                        {
                            if (item.Selected)
                            {
                                arrTo.Add(item);
                            }
                        }
                        foreach (ListItem item in arrTo)
                        {
                            this.lbxSource.Items.Add(item);
                            this.lbxTo.Items.Remove(item);
                        }
                        break;
                    }
                case "AllToLeft":
                    if (this.lbxTo.Items.Count > 0)
                    {
                        foreach (ListItem item in lbxTo.Items)
                        {
                            this.lbxSource.Items.Add(item);
                        }
                        this.lbxTo.Items.Clear();
                        break;
                    }
                    else
                    {
                        break;
                    }
                case "ToUp":
                    for (int i = 0; i < lbxTo.Items.Count; i++)
                    {
                        if (lbxTo.Items[i].Selected)
                        {
                            if (i > 0 && lbxTo.SelectedIndex > -1)
                            {
                                ListItem item = lbxTo.Items[i];
                                lbxTo.Items.RemoveAt(i);
                                lbxTo.Items.Insert(i - 1, item);
                            }
                        }
                    }
                    break;
                case "ToDown":
                    for (int i = lbxTo.Items.Count - 1; i > -1; i--)
                    {
                        if (lbxTo.Items[i].Selected)
                        {
                            ListItem item = lbxTo.Items[i];
                            if (i < lbxTo.Items.Count - 1)
                            {
                                lbxTo.Items.Insert(i + 2, item);
                                lbxTo.Items.RemoveAt(i);
                            }
                        }
                    }
                    break;
            }
        }

沒有留言:

張貼留言