从数据库中查询出所有用户名,我想在每个用户名前面加一个复选框,可以选择任意几个,用gridview吧?要是固定的就简单了,把任意选中的用户名显示在label上就可了,初学不知怎么实现,

解决方案 »

  1.   

        <form id="form1" runat="server">
        <div>
            <asp:CheckBoxList ID="cboxList" runat="server">
                <asp:ListItem Text="张三" Value="1"></asp:ListItem>
                <asp:ListItem Text="李四" Value="2"></asp:ListItem>
                <asp:ListItem Text="王五" Value="3"></asp:ListItem>
                <asp:ListItem Text="赵六" Value="4"></asp:ListItem>
            </asp:CheckBoxList>
        </div>
        <div>
            <asp:Button ID="btnSubmit" runat="server" Text="显示" onclick="btnSubmit_Click" />
        </div>
        <div>
            <asp:Label ID="lblUserName" runat="server" Text=""></asp:Label>
        </div>
        </form>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserName();
            }
        }    private void BindUserName()
        {
            //数据源
            var list = new[]{
                new {Text="张三",Value="1"},
                new {Text="李四",Value="2"},
                new {Text="王五",Value="3"},
                new {Text="赵六",Value="4"}
            }.ToList();        //绑定数据
            cboxList.DataSource = list;
            cboxList.DataTextField = "Text";
            cboxList.DataValueField = "Value";
            cboxList.DataBind();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = string.Empty;        foreach (ListItem cbox in cboxList.Items)
            {
                if (cbox.Selected)
                {
                    userName += cbox.Text + "-";
                }
            }
            lblUserName.Text = userName.TrimEnd('-');    }
      

  2.   


    如果只显示checkbox+用户名的组合,建议用checkboxlist如果显示项不取这么多可以选择用repeater控件ItemCommand事件中去处理你要做的事
      

  3.   

    GridView设置模板列,其中一列放复选框,应该不难的。
      

  4.   

    就是不知道有多少用户,有可能几个,也可能几十个,甚至几百个,我觉得3楼比较符合我的意思,几位大侠能否给个具体的例子。GridView设置模板列,其中一列放复选框这些都不难,可是怎么表示其中某一个被选中了呢,这些复选框跟普通复选框有区别吗?
      

  5.   

    前端aspx页面:
    <script type="text/javascript" language="javascript">
    function CheckItem(Text)
    {
        var form=document.forms[0];
        var j=0;
        for( i=0;i<form.elements.length; i++)
        {
           if( form.elements[i].checked && form.elements[i].id!="chkAll")
           {
                j=1;
                break;
           }
        }
        if(j==1)
        {
           return confirm(Text);
        } 
        else
        {
           alert("请选择项!")
           return false;
        }
    }
        </script>
    <asp:GridView ID="gvDictionary" BorderColor="#C1DAD7" runat="server" Width="100%"
                                    AutoGenerateColumns="False" SkinID="gridviewSkin1" DataKeyNames="DictionaryId">
                                    <Columns>
                                        <asp:TemplateField HeaderText="字典编号">
                                            <HeaderStyle HorizontalAlign="Center" />
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkDictionaryId" runat="server" />
                                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DictionaryID") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="DictTypeTitle" HeaderText="字典类别">
                                            <HeaderStyle HorizontalAlign="Center" />
                                            <ItemStyle HorizontalAlign="Center" Width="120px" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="DictText" HeaderText="显示值">
                                            <ItemStyle HorizontalAlign="Center" />
                                            <HeaderStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="DictValue" HeaderText="选中值">
                                            <ItemStyle HorizontalAlign="Center" />
                                            <HeaderStyle HorizontalAlign="Center" />
                                        </asp:BoundField>
                                        <asp:TemplateField HeaderText="编辑">
                                            <HeaderStyle HorizontalAlign="Center" />
                                            <EditItemTemplate>
                                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <img src="../tab/images/edt.gif" width="16" height="16" />
                                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "Edit.aspx?act=edit&id="+Eval("DictionaryId") %>'>编辑</asp:HyperLink>
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" />
                                        </asp:TemplateField>
                                    </Columns>
                                    <HeaderStyle CssClass="headbackground" BorderColor="#C1DAD7" />
                                    <RowStyle BorderColor="#C1DAD7" Height="23px" />
                                    <EmptyDataTemplate>
                                        不存在记录!
                                    </EmptyDataTemplate>
    </asp:GridView>
    <asp:Button ID="btnDel" runat="server" CssClass="btnnoborder" OnClick="btnDel_Click"
                                                                            Text="删除" OnClientClick="return CheckItem('你确定删除选中项吗?')" />服务端cs页面:
    protected void btnDel_Click(object sender, EventArgs e)
            {
                int i = 0;
                foreach (GridViewRow myItem in gvDictionary.Rows)
                {                CheckBox chkSel = (CheckBox)myItem.FindControl("chkDictionaryId");
                    if (chkSel != null)
                    {                    if (chkSel.Checked)
                        {
                            i = i + 1;
                            bll.Delete(int.Parse(gvDictionary.DataKeys[myItem.RowIndex][0].ToString()));
                        }
                    }
                }
                if (i == 0)
                {
                    Type csType = this.GetType();
                    ClientScript.RegisterStartupScript(csType, "No choose Error", "<script language=JavaScript>alert('请选择要操作的项目!');</script>");
                }
                BindDictionary();
            }