Lee_Goophy

导航

ListBox用法,左右两个lb,中间两个btn,点选按钮左边的内容向右边的添加

ListBox的运用,点击“添加”可以将左边的ListBox内容添加到右边ListBox中

2009-03-26 09:31

前台页面代码:

  <asp:Panel ID="Panel1" runat="server" Height="50px" Visible="false">
                    <div id="div2" >
        <table border="0" cellpadding="0" cellspacing="0" >
            <tr>
                <td width="10px">&nbsp;</td>
                <td valign="top" style="height: 328px; width:130px;">
                    <font size="2" color="blue">表中字段</font><br />
                    <asp:ListBox ID="ListBoxLeft" runat="server" Width="130px" Height="351px"></asp:ListBox>
                </td>
                <td valign="middle" style="text-align:center;width:30px;height: 328px">
                    <table>
       <tr>
        <td>
           <asp:Button ID="Button1" Runat="server" Text=">" Width="25px" Font-Bold="True" CommandName="addone" onclick="AddOneOrAllBtn_Click"></asp:Button>
         <br>
        </td>
       </tr>
       <tr>
        <td style="height: 22px">
           <asp:Button ID="Button2" Runat="server" Text=">>" Width="25px" Font-Bold="True" Font-Italic="True" CommandName="addall" onclick="AddOneOrAllBtn_Click"></asp:Button></td>
       </tr>
      </table>
                </td>
                <td valign="top" style="height:328px; width:130px;">
                     <font size="2" color="blue">显示字段</font><br />
                     <asp:ListBox ID="ListBoxRight" runat="server" Width="130px" Height="351px"></asp:ListBox>
                </td>
                <td style="height:328px; width:30px; text-align:center">
                    <asp:imagebutton id="DeleteBtn" runat="server" ImageUrl="~/images/delete.gif" AlternateText="删除此项" CommandName="delete" OnClick="DeleteBtn_Click"></asp:imagebutton>
                </td>
            </tr>         
        </table>
        <asp:Button id="SubmitBtn" Text="修改" runat="server" OnClick="SubmitBtn_Click" />   
        <asp:Button ID="combtn" Text="完成" runat="server" OnClick="combtn_Click" />
    </div>
                    </asp:Panel>

后台代码:

protected void AddOneOrAllBtn_Click(object sender, EventArgs e)
    {
        String commandName = ((Button)sender).CommandName;//获取控件的命令参数
        switch (commandName)
        {
            case "addone":
                {
                    AddOne();//方式1:一次添加一个角色
                    break;
                }
            case "addall":
                {
                    AddAll();//方式2:一次添加所有角色
                    break;
                }
            default:
                {
                    break;
                }
        }
    }

    private void AddOne() //一次增加一个
    {
        if (ListBoxLeft.SelectedIndex > -1)
        {
            if (IsExistField(ListBoxRight, ListBoxLeft.SelectedItem) == false)
            {
                ListBoxRight.Items.Add(ListBoxLeft.SelectedItem);
            }
            else
            {
                Response.Write("<script>alert(\"您选择的字段已经存在,请重新选择!\")</script>");
            }
        }
        else
        {
            Response.Write("<script>alert(\"请选择您要添加的字段!\")</script>");
        }

        //必须清空列表中的选项;原因在于,网页不支持同时选定两个ListBox
        foreach (ListItem ls in ListBoxRight.Items)
        {
            if (ls.Selected == true)
            {
                ls.Selected = false;
            }
        }
    }

    private void AddAll() //一次增加所有的
    {
        ListBoxRight.Items.Clear();

        if (ListBoxLeft.Items.Count > 0)
        {
            foreach (ListItem ls in ListBoxLeft.Items)
            {
                ListBoxRight.Items.Add(ls);
            }
        }
        else
        {
            Response.Write("<script>alert(\"请选择您的数据项为空!\")</script>");
        }
    }

    private bool IsExistField(ListBox listBox, ListItem listItem) //检查是右边是否存在所要增加的
    {
        bool isExist = false;
        if (listBox.Items.Count > 0)
        {
            foreach (ListItem ls in listBox.Items)
            {
                if (ls.Text == listItem.Text)
                {
                    isExist = true;
                    break;
                }
            }
        }
        return isExist;
    }

    protected void DeleteBtn_Click(object sender, ImageClickEventArgs e) //删除按钮
    {
        if (this.ListBoxRight.SelectedIndex > -1)
        {
            this.ListBoxRight.Items.Remove(this.ListBoxRight.SelectedItem);
        }
        else
        {
            Response.Write("<script>alert(\"请选择您要删除的用户角色!\")</script>");
        }
    }

posted on 2011-10-17 18:49  Lee_Goophy  阅读(466)  评论(1)    收藏  举报