GridView和CheckBox结合实现可选择删除
前台代码:
后台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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 runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center; height: 640px">
<table class="style1">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Horizontal">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="department" HeaderText="专业" />
<asp:BoundField DataField="grade" HeaderText="年级" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td align="center" >
<asp:CheckBox ID="CheckBox2" runat="server" Text="全选"
oncheckedchanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
<asp:Button ID="Button2" runat="server" Text="取消" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
</td>
<td >
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!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 runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center; height: 640px">
<table class="style1">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Horizontal">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="department" HeaderText="专业" />
<asp:BoundField DataField="grade" HeaderText="年级" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td align="center" >
<asp:CheckBox ID="CheckBox2" runat="server" Text="全选"
oncheckedchanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
<asp:Button ID="Button2" runat="server" Text="取消" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
</td>
<td >
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from information", conn);
DataSet dataset = new DataSet();
adp.Fill(dataset, "information");
conn.Close();
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new String[] { "id" };
GridView1.DataBind();
}
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 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;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlCommand comm = new SqlCommand("delete from information where id='" + GridView1.DataKeys[i].Value + "'", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
Bind();
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from information", conn);
DataSet dataset = new DataSet();
adp.Fill(dataset, "information");
conn.Close();
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new String[] { "id" };
GridView1.DataBind();
}
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 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;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlCommand comm = new SqlCommand("delete from information where id='" + GridView1.DataKeys[i].Value + "'", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
Bind();
}
}