GridView模板列中添加CheckBox 实现全选、删除
2008-09-26 14:01
591 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>GridView模板列中添加CheckBox 实现全选、删除</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" Font-Size="9pt" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="用户姓名" />
<asp:BoundField DataField="Address" HeaderText="家庭住址" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" />
<asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlConn;
SqlCommand sqlComm;
string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
private void databind()
{
string strSql = "select * from myDt";
sqlConn = new SqlConnection(strConn);
SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
DataSet ds = new DataSet();
sqlConn.Open();
sqlDa.Fill(ds, "myDt");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
sqlConn.Close();
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox2.Checked = false;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
chk.Checked = false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
sqlConn = new SqlConnection(strConn);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if(chk.Checked == true)
{
string strSql = "delete myDt where ID = '" + GridView1.DataKeys[i].Value + "'";
sqlComm = new SqlCommand(strSql, sqlConn);
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}
}
databind();
}
}
<!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>GridView模板列中添加CheckBox 实现全选、删除</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" Font-Size="9pt" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="用户姓名" />
<asp:BoundField DataField="Address" HeaderText="家庭住址" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" />
<asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlConn;
SqlCommand sqlComm;
string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
private void databind()
{
string strSql = "select * from myDt";
sqlConn = new SqlConnection(strConn);
SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
DataSet ds = new DataSet();
sqlConn.Open();
sqlDa.Fill(ds, "myDt");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
sqlConn.Close();
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox2.Checked = false;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
chk.Checked = false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
sqlConn = new SqlConnection(strConn);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if(chk.Checked == true)
{
string strSql = "delete myDt where ID = '" + GridView1.DataKeys[i].Value + "'";
sqlComm = new SqlCommand(strSql, sqlConn);
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}
}
databind();
}
}
相关文章推荐
- GridView模板中用Checkbox实现全选删除
- GridView 添加checkBox 实现全选功能
- C# GridView列头添加CheckBox控件实现全选功
- GridView控件与CheckBox结合,实现全选功能,同时实现删除选中的多行数据
- 在GridView实现批量删除记录、CheckBox全选
- gridview checkbox 多选 全选 全不选 反选 删除 实现
- JavaScript实现GridView中添加CheckBox列(全选与否)
- Gridview中的添加CheckBox实现全选
- GridView控件中模板列CheckBox全选、反选、取消实现代码
- GridView控件中模板列CheckBox全选、反选、取消实现代码
- Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能
- Jquery 实现动态添加table tr 和删除tr 以及checkbox的全选 和 获取添加TR删除TR后的数据
- GridView控件中添加CheckBox控件,并且实现选择、全选和反选
- JS在GridView中实现CheckBox全选和非全选 及Gridview批量删除使用技巧
- GridView控件与CheckBox结合,实现全选功能,同时实现删除选中的多行数据 .
- JavaScript实现GridView中添加CheckBox列(全选与否)
- ASP.NET Gridview与checkbox全选、全不选实现代码
- AngularJS购物车实现添加用户,修改用户,全选反选,批量删除
- 利用jQuery实现全选、反选、删除和添加
- GridView和CheckBox连用,实现全选,分页,并且分页之后选择的复选框的状态依然存在