2010年5月25日 星期二

[ASP.NET]左右ListBox選單(1) -- 按鈕Trigger

以ListBox實做左方ListBox為可選擇之項目,右方ListBox為已選擇之項目,中間放置四個按鈕,分別為向右鈕/全選向右鈕/向左鈕/全選向左鈕。




Sample.aspx
<table align="center" class="style1" style="width: 400px">
    <tr>
        <td align="left" style="font-weight: bold; font-size: 13px; font-family: Arial" width="150px">
            可選擇的ProductID </td>
        <td>
            &nbsp; </td>
        <td align="left" style="font-weight: bold; font-size: 13px; font-family: Arial">
            已選擇的ProductID </td>
    </tr>
    <tr>
        <td align="left">
            <asp:ListBox ID="ListBoxLeft" runat="server" Rows="5" SelectionMode="Multiple" Width="150px">
                <asp:ListItem>AAAA</asp:ListItem>
                <asp:ListItem>BBBB</asp:ListItem>
                <asp:ListItem>CCCC</asp:ListItem>
                <asp:ListItem>DDDD</asp:ListItem>
            </asp:ListBox>
        </td>
        <td align="center">
            <asp:Button ID="ButtonRight" runat="server" OnClick="ButtonRight_Click" Text="Right"
                Width="70px" CssClass="barkbutton" />
            <br />
            <asp:Button ID="ButtonRightAll" runat="server" OnClick="ButtonRightAll_Click" Text="ALL Right"
                Width="70px" CssClass="barkbutton" />
            <br />
            <asp:Button ID="ButtonLeft" runat="server" OnClick="ButtonLeft_Click" Text="Left"
                Width="70px" CssClass="barkbutton" />
            <br />
            <asp:Button ID="ButtonLeftAll" runat="server" OnClick="ButtonLeftAll_Click" Text="ALL Left"
                Width="70px" CssClass="barkbutton" />
        </td>
        <td align="left">
            <asp:ListBox ID="ListBoxRight" ondblclick="__doPostBack('ButtonLeft','');" runat="server"
                Rows="5" SelectionMode="Multiple" Width="150px"></asp:ListBox>
        </td>
    </tr>
</table>
Sample.aspx.cs

protected void ButtonRight_Click(object sender, EventArgs e)
    {
        if (ListBoxLeft.SelectedItem != null)
        {
            foreach (ListItem item in ListBoxLeft.Items)
            {
                if (item.Selected == true)
                {
                    ListBoxRight.Items.Add(item);
                }
            }

            foreach (ListItem item in ListBoxRight.Items)
            {
                if (item.Selected == true)
                {
                    ListBoxLeft.Items.Remove(item);
                }
            }
        }

    }

    protected void ButtonRightAll_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in ListBoxLeft.Items)
        {
            ListBoxRight.Items.Add(item);
        }
        ListBoxLeft.Items.Clear();
    }

    protected void ButtonLeft_Click(object sender, EventArgs e)
    {
        if (ListBoxRight.SelectedItem != null)
        {
            foreach (ListItem item in ListBoxRight.Items)
            {
                if (item.Selected == true)
                {
                    ListBoxLeft.Items.Add(item);
                }
            }

            foreach (ListItem item in ListBoxLeft.Items)
            {
                if (item.Selected == true)
                {
                    ListBoxRight.Items.Remove(item);
                }
            }
        }
    }

    protected void ButtonLeftAll_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in ListBoxRight.Items)
        {
            ListBoxLeft.Items.Add(item);
        }
        ListBoxRight.Items.Clear();
    } 
[ASP.NET]左右ListBox選單(1) -- 按鈕Trigger
[ASP.NET]左右ListBox選單(2) -- Javascript Trigger
[ASP.NET]左右ListBox選單(3) -- 非同步PostBack Trigger

沒有留言:

張貼留言