天天看點

GridView和CheckBox結合使用

1.前台代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewCheckbox.aspx.cs" Inherits="GridViewCheckbox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>無标題頁</title>

    <link href="css.css" target="_blank" rel="external nofollow" rel="stylesheet" type="text/css" />

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" Width="544px" AutoGenerateColumns="False">

            <Columns>

                <asp:TemplateField>

                                <ItemTemplate>

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

                                </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField DataField="身份證号碼" HeaderText="使用者ID" />

                <asp:BoundField DataField="姓名" HeaderText="使用者姓名" />

                <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />

            </Columns>

            <RowStyle Horiz VerticalAlign="Middle" />

            <EditRowStyle Horiz VerticalAlign="Middle" />

            <HeaderStyle BackColor="#C0FFC0" />

            <AlternatingRowStyle Horiz VerticalAlign="Middle" />

        </asp:GridView>

        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"

            Text="全選" />

        <asp:Button ID="Button1" runat="server" Height="18px"  Text="删 除" />

        <asp:Button ID="Button2" runat="server" Height="18px"  Text="取 消" /></div>

   </form>

</body>

</html>

2.背景代碼

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class GridViewCheckbox : System.Web.UI.Page

{

    SqlConnection sqlcon;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            bind();

        }

    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)

    {

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

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (CheckBox2.Checked == true)

            {

                cbox.Checked = true;

            }

            else

            {

                cbox.Checked = false;

            }

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        sqlcon = DB.createCon();

        SqlCommand sqlcom;

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

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)

            {

                string sqlstr = "delete from Employee where 身份證号碼='" + GridView1.DataKeys[i].Value + "'";

                sqlcom = new SqlCommand(sqlstr, sqlcon);

                sqlcon.Open();

                sqlcom.ExecuteNonQuery();

                sqlcon.Close();

            }

        }

        bind();

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        CheckBox2.Checked = false;

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

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            cbox.Checked = false;

        }

    }

    public void bind()

    {

        string sqlstr = "select top 4 * from Employee";

        sqlcon = DB.createCon();

        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

        DataSet myds = new DataSet();

        sqlcon.Open();

        myda.Fill(myds, "Employee");

        GridView1.DataSource = myds;

        GridView1.DataKeyNames = new string[] { "身份證号碼" };

        GridView1.DataBind();

        sqlcon.Close();

    }

}

GridView和CheckBox結合使用

轉載于:https://www.cnblogs.com/Shirly-Zhang/p/5231675.html