GridView控件与CheckBox结合,实现全选功能,同时实现删除选中的多行数据 .

时间:2021-07-19 10:55:18
 页面设计:

<table style="border:solid 1px green" width="35%">

            <tr>

                <td style="background-color:Fuchsia;">

                    <strong>

                        <span>

                            GridView控件与checkbox控件结合

                        </span>

                    </strong>

                </td>

            </tr>

            <tr>

                <td >

                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="stuid">

                        <Columns>

                            <asp:TemplateField >   

                                <HeaderTemplate>

                                    <asp:CheckBox ID="chAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="chAll_CheckedChanged" />

                                </HeaderTemplate>                                                         

                                <ItemTemplate>

                                    <asp:CheckBox ID="cbo" runat="server" />

                                </ItemTemplate>

                            </asp:TemplateField>

                            <asp:BoundField DataField="stuid" HeaderText="学生编号" />

                            <asp:BoundField DataField="stuname" HeaderText="姓名" />

                            <asp:BoundField DataField="stuadd" HeaderText="地址" />

                            <asp:BoundField DataField="stucity" HeaderText="城市" />

                        </Columns>

                        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

                        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

                        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

                        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

                    </asp:GridView>                   

                </td>

            </tr>

            <tr>

                <td >

                    <asp:Button ID="btndel" runat="server" Text="删除" OnClick="btndel_Click" />

                    <asp:Button ID="btncancel" runat="server" Text="取消选择" OnClick="btncancel_Click" />

                </td>

            </tr>

        </table> 

后代代码:

/// <summary>

    /// 全选

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void chAll_CheckedChanged(object sender, EventArgs e)

    {

        foreach (GridViewRow gr in GridView1.Rows)

        {

            CheckBox ch = (CheckBox)gr.FindControl("cbo");

            //相互变幻

            if (ch.Checked == true)

            {

                ch.Checked = false;

            }

            else

            {

                ch.Checked = true;

            }

        }

    }

    //取消选择

    protected void btncancel_Click(object sender, EventArgs e)

    {

        foreach (GridViewRow gr in GridView1.Rows)

        {

            CheckBox ch = (CheckBox)gr.FindControl("cbo");

            //取消选择

            ch.Checked = false;

        }

    }

    //删除

    protected void btndel_Click(object sender, EventArgs e)

    {

        btndel.Attributes.Add("onclick","javascript:return confirm('确定删除吗?');");

        //进行相关操作

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);

        con.Open();

        //for(int i=0;i<=GridView1.Rows.Count-1;i++)

        //{

        foreach(GridViewRow gr in GridView1.Rows)

        {            

            CheckBox ch = (CheckBox)gr.FindControl("cbo");

            if (ch.Checked == true)

            {

                //定义删除行的字符串

                string cmdtext = "delete from student where stuid="+GridView1.DataKeys[gr.RowIndex].Value.ToString();

                //创建命令对象

                SqlCommand cmd = new SqlCommand(cmdtext,con);

                //执行删除操作

                cmd.ExecuteNonQuery();

            }

        }

        //关闭数据库连接

        con.Close();

        //回绑数据

        BindGridView();

        Response.Write("<script>alert('删除成功。');</script>");

    }