• 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" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <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>
            &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
            <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();
        }
    }

  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/Shirly-Zhang/p/5231675.html
Copyright © 2020-2023  润新知