码迷,mamicode.com
首页 > 其他好文 > 详细

GridView和CheckBox结合使用

时间:2016-03-01 17:14:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

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

GridView和CheckBox结合使用

标签:

原文地址:http://www.cnblogs.com/Shirly-Zhang/p/5231675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!