1.通过GridView的属性:DataKeyNames来获取主键;
2.遍历数据行,获取选中的CheckBox 所属的行的主键.(有点绕口...慢慢看...)
3.拼接SQL语句; Delete 表名 where id in(XX,XX,XX);
Default.aspx页完整代码:
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Button ID="btnDelete" runat="server" Text="删除选中项" OnClick="btnDelete_Click"
Style="height: 21px" />
<br />
<br />
<%-- 通过给GridView1添加属性:DataKeyNames来获取主键;--%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Width="881px" DataKeyNames="id">
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="uname" HeaderText="名称" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</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>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Button ID="btnDelete" runat="server" Text="删除选中项" OnClick="btnDelete_Click"
Style="height: 21px" />
<br />
<br />
<%-- 通过给GridView1添加属性:DataKeyNames来获取主键;--%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Width="881px" DataKeyNames="id">
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="uname" HeaderText="名称" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs完整代码:
View Code
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetData();
}
}
//绑定数据
protected void GetData()
{
string sql = "select * from userinfo";
GridView1.DataSource = SQLHelper.GetDateSet(sql, CommandType.Text);
GridView1.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//sqlText用于拼接SQL语句;
string sqlText = "(";
foreach (GridViewRow objGVR in this.GridView1.Rows)
{
//判断当前行是否为数据行;
if (objGVR.RowType == DataControlRowType.DataRow)
{
CheckBox objCB = objGVR.FindControl("cbxId") as CheckBox;
if (objCB.Checked)
{
//获取选中行的主键;
sqlText += this.GridView1.DataKeys[objGVR.RowIndex]["id"].ToString() + ",";
}
}
}
//去掉最后的逗号,并且加上右括号 ,如果不去掉最后一个逗号变会成这样(1,2,3,4,5,6,)
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete userinfo where id in" + sqlText;
Response.Write("拼接后的SQL语句为:" + sqlText);
//执行删除语句
int delCount = Convert.ToInt32(SQLHelper.ExecuteNonQuery(sqlText, CommandType.Text));
Response.Write("共删除数据:" + delCount + "条");
this.GetData();
}
}
}
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetData();
}
}
//绑定数据
protected void GetData()
{
string sql = "select * from userinfo";
GridView1.DataSource = SQLHelper.GetDateSet(sql, CommandType.Text);
GridView1.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//sqlText用于拼接SQL语句;
string sqlText = "(";
foreach (GridViewRow objGVR in this.GridView1.Rows)
{
//判断当前行是否为数据行;
if (objGVR.RowType == DataControlRowType.DataRow)
{
CheckBox objCB = objGVR.FindControl("cbxId") as CheckBox;
if (objCB.Checked)
{
//获取选中行的主键;
sqlText += this.GridView1.DataKeys[objGVR.RowIndex]["id"].ToString() + ",";
}
}
}
//去掉最后的逗号,并且加上右括号 ,如果不去掉最后一个逗号变会成这样(1,2,3,4,5,6,)
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete userinfo where id in" + sqlText;
Response.Write("拼接后的SQL语句为:" + sqlText);
//执行删除语句
int delCount = Convert.ToInt32(SQLHelper.ExecuteNonQuery(sqlText, CommandType.Text));
Response.Write("共删除数据:" + delCount + "条");
this.GetData();
}
}
}
如需全选功能请参见:http://www.cnblogs.com/zhuiyi/archive/2011/06/27/2091738.html