• 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();
        }
    }

  • 相关阅读:
    使用Python和百度对外共享的TTS接口实现文本转语音
    最简单的AI代码——价值“一个亿”
    【CSS】让滚动条消失
    足球圈移动端网页Demo
    golang通过errgroup单chan多个grouting通讯样例
    gin返回前端excel文件流
    xorm构建复杂sql
    CSS实现标签闪动效果
    vue_js遍历双重数组对象
    vue_html实现加载双重数组对象
  • 原文地址:https://www.cnblogs.com/Shirly-Zhang/p/5231675.html
Copyright © 2020-2023  润新知