asp.Netでは2つのListBoxが左右に移動し,上下に移動する.

5204 ワード

        <table>
            <tr>
                <td>
                    <asp:ListBox ID="leftListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                        <asp:ListItem Text="  " Value="  "></asp:ListItem>
                    </asp:ListBox>
                </td>
                <td>
                    <asp:Button ID="btnLeftToRight" runat="server" Text=">" OnClick="btnLeftToRight_Click" /><br />
                    <asp:Button ID="btnRightToLeft" runat="server" Text="<" OnClick="btnRightToLeft_Click" /><br />
                    <asp:Button ID="btnRightUp" runat="server" Text="∧" OnClick="btnRightUp_Click" /><br />
                    <asp:Button ID="btnRightDown" runat="server" Text="∨" OnClick="btnRightDown_Click" /><br />
                </td>
                <td>
                    <asp:ListBox ID="rightListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">
                    </asp:ListBox>
                </td>
            </tr>
        </table>

 
    //  
    protected void btnLeftToRight_Click(object sender, EventArgs e)
    {
        //          
        ArrayList arrRight = new ArrayList();
        //    listbox item      
        foreach (ListItem item in this.leftListBox.Items)
        {
            if (item.Selected)
            {
                arrRight.Add(item);
            }
        }
        //        
        foreach (ListItem item in arrRight)
        {
            this.rightListBox.Items.Add(item);
            this.leftListBox.Items.Remove(item);
        }  
    }

    //  
    protected void btnRightToLeft_Click(object sender, EventArgs e)
    {
        ArrayList arrLeft = new ArrayList();
        //    listboxitem      
        foreach (ListItem item in this.rightListBox.Items)
        {
            if (item.Selected)
            {
                arrLeft.Add(item);
            }
        }
        //        
        foreach (ListItem item in arrLeft)
        {
            this.leftListBox.Items.Add(item);
            this.rightListBox.Items.Remove(item);
        }  
    }

    //  
    protected void btnRightUp_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex > 0)
            {
                int idx = rightListBox.SelectedIndex;
                var SelectedItem = rightListBox.SelectedItem;
                rightListBox.Items.Insert(rightListBox.SelectedIndex - 1, new ListItem(SelectedItem.Text, SelectedItem.Value));
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex);
                rightListBox.SelectedIndex = idx - 1;
            }
        }
        catch (Exception)
        {
            Response.Write("<script language =javascript>alert('     !')</script>");
        }  
    }

    //  
    protected void btnRightDown_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex < rightListBox.Items.Count - 1)
            {
                int idx = rightListBox.SelectedIndex;
                rightListBox.Items.Insert(rightListBox.SelectedIndex, rightListBox.Items[rightListBox.SelectedIndex + 1]);
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex + 1);
                rightListBox.SelectedIndex = idx + 1;
            }
        }
        catch (Exception)
        {
            Response.Write("<script language =javascript>alert('     !')</script>");
        }  
    }