GridView和CheckBox连用,实现全选

 <Columns>
           <asp:TemplateField HeaderText="全选">
           <ItemTemplate>
           <asp:CheckBox ID="cb" runat ="server" />
           </ItemTemplate>
           <HeaderTemplate>
            <asp:CheckBox ID="quanxuan" runat="server" Text="全选" OnCheckedChanged="quanxuan_CheckedChanged" AutoPostBack="true"/>
           </HeaderTemplate>
           </asp:TemplateField>

 

 

--cs文件

 protected void quanxuan_CheckedChanged(object sender, EventArgs e)
    {

        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = gr.FindControl("cb") as CheckBox;
            if (cb.Checked == false)
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
        }

    }

posted @ 2012-02-23 14:03  玉面麒麟  Views(185)  Comments(0Edit  收藏  举报