在网上看到一些批量删除数据的例子.但是大部分是用的数组来保存所选的id.这样比较麻烦,也不容易被理解.我就拿个比较简单的方法来实现这个功能.(只看红色的部分即可,而且还加个删除提示窗口)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace overred.test
{
/// <summary>
/// t1 的摘要说明。
/// </summary>
public class t1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label message;
protected System.Web.UI.WebControls.LinkButton showck;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.LinkButton delall;
protected System.Web.UI.WebControls.DataGrid dg;
private OleDbCommand cmd;
private OleDbConnection conn;
protected System.Web.UI.WebControls.LinkButton lb;
private string datas;
private void Page_Load(object sender, System.EventArgs e)
{
Session["s"]="dd";
// 在此处放置用户代码以初始化页面
if(!this.Page.IsPostBack)
Sql();
}
private DataSet Getds()
{
datas="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../overred.mdb");
conn = new OleDbConnection(datas);
conn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from subnews order by news_order desc", conn);
DataSet ds = new DataSet();
adp.Fill(ds,"link");
return ds;
}
private void Sql()
{
alter();
using (DataSet dss=Getds())
this.dg.DataSource=dss;
this.dg.DataBind();
}
private void alter()//添加确认窗口
{
this.delall.Attributes.Add("onclick","return confirm('确定删除')");
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.showck.Click += new System.EventHandler(this.showck_Click);
this.delall.Click += new System.EventHandler(this.delall_Click);
this.lb.Click += new System.EventHandler(this.lb_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void showck_Click(object sender, System.EventArgs e)//显示选择的项
{
//ArrayList saveIdChecked=new ArrayList();
CheckBox isChecked;
//遍历每一行
foreach(DataGridItem dataItem in dg.Items)
{
//获取checkbox
isChecked=(CheckBox)dataItem.FindControl("ck");
if(isChecked.Checked==true)
{
string idChecked=dataItem.Cells[1].Text;
Response.Write(idChecked);
Response.Write(isChecked.Checked);
}
}
}
private void delall_Click(object sender, System.EventArgs e)
{
alter();
datas= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../overred.mdb");
conn = new OleDbConnection(datas);
cmd=new OleDbCommand();
cmd.Connection=conn;
conn.Open();
CheckBox isChecked;
//遍历每一行
foreach(DataGridItem dataItem in dg.Items)
{
//获取checkbox
isChecked=(CheckBox)dataItem.FindControl("ck");
if(isChecked.Checked==true)//如果选中则执行删除命令
{
cmd.CommandText="Delete from subnews where news_id="+dataItem.Cells[1].Text.ToString();
cmd.ExecuteNonQuery();
}
}
Sql();//重新绑定
//Response.Redirect("t1.aspx");
}
}
}
aspx:
<%@ Page language="c#" Codebehind="t1.aspx.cs" trace="true" AutoEventWireup="false" Inherits="overred.test.t1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>t1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="dg" runat="server" DataKeyField="news_id">
<Columns>
<ASP:TemplateColumn HeaderText="选择" ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox id="ck" runat="server" />
</ItemTemplate>
</ASP:TemplateColumn>
<asp:BoundColumn HeaderText="id" DataField="news_id" />
<asp:BoundColumn HeaderText="id" DataField="news_name" />
</Columns>
</asp:datagrid>
<P>
<asp:Label id="message" runat="server" />
<asp:LinkButton id="showck" runat="server" Text="删除" ForeColor="navy" Font-Name="verdana" Font-size="12pt" />
</P>
<P>
<asp:LinkButton id="delall" runat="server">delall</asp:LinkButton></P>
<P>
<asp:LinkButton id="lb" runat="server">LinkButton</asp:LinkButton></P>
</FONT>
</form>
</body>
</HTML>
<%@ Page language="c#" Codebehind="t1.aspx.cs" trace="true" AutoEventWireup="false" Inherits="overred.test.t1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>t1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="dg" runat="server" DataKeyField="news_id">
<Columns>
<ASP:TemplateColumn HeaderText="选择" ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox id="ck" runat="server" />
</ItemTemplate>
</ASP:TemplateColumn>
<asp:BoundColumn HeaderText="id" DataField="news_id" />
<asp:BoundColumn HeaderText="id" DataField="news_name" />
</Columns>
</asp:datagrid>
<P>
<asp:Label id="message" runat="server" />
<asp:LinkButton id="showck" runat="server" Text="删除" ForeColor="navy" Font-Name="verdana" Font-size="12pt" />
</P>
<P>
<asp:LinkButton id="delall" runat="server">delall</asp:LinkButton></P>
<P>
<asp:LinkButton id="lb" runat="server">LinkButton</asp:LinkButton></P>
</FONT>
</form>
</body>
</HTML>
<%@ Page language="c#" Codebehind="t1.aspx.cs" trace="true" AutoEventWireup="false" Inherits="overred.test.t1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>t1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="dg" runat="server" DataKeyField="news_id">
<Columns>
<ASP:TemplateColumn HeaderText="选择" ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox id="ck" runat="server" />
</ItemTemplate>
</ASP:TemplateColumn>
<asp:BoundColumn HeaderText="id" DataField="news_id" />
<asp:BoundColumn HeaderText="id" DataField="news_name" />
</Columns>
</asp:datagrid>
<P>
<asp:Label id="message" runat="server" />
<asp:LinkButton id="showck" runat="server" Text="删除" ForeColor="navy" Font-Name="verdana" Font-size="12pt" />
</P>
<P>
<asp:LinkButton id="delall" runat="server">delall</asp:LinkButton></P>
<P>
<asp:LinkButton id="lb" runat="server">LinkButton</asp:LinkButton></P>
</FONT>
</form>
</body>
</HTML>
说明:
dataItem.Cells[1].Text.ToString()为news_id