求救 本帖最后由 chen57298067 于 2011-05-16 09:54:28 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if (cb != null && cb.Checked) GridView1的Rows可能包含表头,表头的话必然是找不到CheckBox所以,在使用cb之前,要做一个判断。foreach (GridViewRow row in GridView1.Rows){ CheckBox cb = (CheckBox)row.FindControl("Checkbox1"); if (cb!=null && cb.Checked) { strid = GridView1.DataKeys[row.RowIndex].Value.ToString(); string sql = "delete from class where id= ' " + strid + " ' "; // 数据库操作 } }你的括号好像有点问题,对不起来,我自己改了一下。这里还有一个更优化的方法,循环调用删除的,每次调用都需要获取一个数据库连接,然后调用一段sql,这样的效率比较低,比如先拼接id串,这样只有一次获取数据库连接和释放连接的损耗。string idlist = "";foreach (GridViewRow row in GridView1.Rows){ CheckBox cb = (CheckBox)row.FindControl("Checkbox1"); if (cb!=null && cb.Checked) { idlist += GridView1.DataKeys[row.RowIndex].Value.ToString() + ","; } }if(idlist.endWith(",")) idlist = idlist.Substring(0, idlist.length - 1);string sql = "delete from class where id in ( ' " + idlist + " ' )";// 数据库操作 没有找到 Checkbox1 这个控件 看你控件id对不 string sql = "delete from class where id= ' " + strid + " ' ";放到foreach循环中试一下,貌似你的strid一直被覆盖 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } private void bind() { DataSet ds = null; ds = DAL.SQLHelper.getDt("select top(13)pid, pname from pinglun"); this.GridView1.DataSource = ds; this.GridView1.DataKeyNames = new string[] { "pid" }; this.GridView1.DataBind(); ViewState["ck"] = null; } protected void Button1_Click(object sender, EventArgs e) { ViewState["ck"] = null; for (int i = 0; i < this.GridView1.Rows.Count;i++ ) { if (((CheckBox)(this.GridView1.Rows[i].FindControl("CheckBox1"))).Checked) { ViewState["ck"] += ((Label)(this.GridView1.Rows[i].FindControl("Label1"))).Text+","; } } getID(); } private void getID() { if (ViewState["ck"] != null) { string idlist = ViewState["ck"].ToString(); idlist = idlist.Remove(idlist.Length - 1, 1); string sql = "delete from pinglun where pid in( " + idlist + ")"; if (DAL.SQLHelper.ExecuteNonQuery(sql) == 1) { ScriptManager.RegisterStartupScript(this, this.GetType(), "alt", "alert('删除成功!');", true); bind(); } } else { ScriptManager.RegisterStartupScript(this,this.GetType(), "alt", "alert('您没有任何选择!');", true); } } Button1_Click就是你的删除事件。 MVC,实体保存问题 怎么玩转csdn? 100分请教判断上传图片是否存在 (C#) 移除控件属性的问题 如何使.net生成文本? 用frame分隔开的页面的form验证的问题----高分跪求,已经两天了,项目等着release呢! 如何消除登录留下的信息? 这段操作数据库的代码错了 帮忙看看~ 关于Global中的Redirect问题 请教在vb写asp.net程序中判断空值的方法? 如何把default.aspx页面dropdownList被选中的数据传给default.aspx.cs文件 用asp.net怎么把服务器上的一段文件夹和其中的文件及子文件夹和文件放到本地
{
CheckBox cb = (CheckBox)row.FindControl("Checkbox1");
if (cb!=null && cb.Checked)
{
strid = GridView1.DataKeys[row.RowIndex].Value.ToString();
string sql = "delete from class where id= ' " + strid + " ' ";
// 数据库操作
}
}
你的括号好像有点问题,对不起来,我自己改了一下。这里还有一个更优化的方法,循环调用删除的,每次调用都需要获取一个数据库连接,然后调用一段sql,这样的效率比较低,比如先拼接id串,这样只有一次获取数据库连接和释放连接的损耗。
string idlist = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("Checkbox1");
if (cb!=null && cb.Checked)
{
idlist += GridView1.DataKeys[row.RowIndex].Value.ToString() + ",";
}
}
if(idlist.endWith(","))
idlist = idlist.Substring(0, idlist.length - 1);
string sql = "delete from class where id in ( ' " + idlist + " ' )";
// 数据库操作
放到foreach循环中试一下,貌似你的strid一直被覆盖
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
} private void bind()
{
DataSet ds = null;
ds = DAL.SQLHelper.getDt("select top(13)pid, pname from pinglun");
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "pid" };
this.GridView1.DataBind();
ViewState["ck"] = null;
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["ck"] = null;
for (int i = 0; i < this.GridView1.Rows.Count;i++ )
{ if (((CheckBox)(this.GridView1.Rows[i].FindControl("CheckBox1"))).Checked)
{
ViewState["ck"] += ((Label)(this.GridView1.Rows[i].FindControl("Label1"))).Text+",";
}
}
getID(); } private void getID()
{
if (ViewState["ck"] != null)
{
string idlist = ViewState["ck"].ToString();
idlist = idlist.Remove(idlist.Length - 1, 1);
string sql = "delete from pinglun where pid in( " + idlist + ")";
if (DAL.SQLHelper.ExecuteNonQuery(sql) == 1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alt", "alert('删除成功!');", true);
bind();
}
}
else
{
ScriptManager.RegisterStartupScript(this,this.GetType(), "alt", "alert('您没有任何选择!');", true);
}
}