zoukankan      html  css  js  c++  java
  • 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();
        }
    }

  • 相关阅读:
    mysql 配置
    idea 学会看log文件
    ac自动机(tree+kmp模板)
    矩阵快速幂(纯数学递推)
    矩阵快速幂(queue递推)
    RMQ(连续相同最大值)
    dp(过河问题)
    bfs(火星撞地球)
    相同子序列集合
    图博弈
  • 原文地址:https://www.cnblogs.com/Shirly-Zhang/p/5231675.html
Copyright © 2011-2022 走看看