日期:2014-05-18  浏览次数:20499 次

为什么我根据ID号来删除,怎么一删就删两个的了
为什么我根据ID号来删除,怎么一删就删两个的了
public       void       Delete_Click(object       sender,       System.EventArgs       e)      
{      
foreach(DataGridItem       Item       in           JDataGrid.Items)      
{                  
if(((CheckBox)Item.FindControl( "chkDel ")).Checked)      
{      
DeleteRow(this.JDataGrid.DataKeys[(int)Item.ItemIndex-1].ToString());      
}      
}  
Operater   operater=new   Operater();
DataSet   ds=operater.executeDataSet(sql);
this.JDataGrid.DataSource=ds;
this.JDataGrid.DataBind();
operater.Close();
}      
//实现删除
private       void       DeleteRow(string   t)      
{      
string       strCon       =       "server=.;database=ipsoon;uid=sa;password= ";      
SqlConnection       conn       =       new       SqlConnection(strCon);      
SqlCommand       Cmd       =       new       SqlCommand( "delete   from   PE_Article   where   ArticleID= ' "+t+ " ' ",conn);      
conn.Open();      
Cmd.ExecuteNonQuery();      
conn.Close();            
}

------解决方案--------------------
察看运行出来的页面的代码源,可以发现CheckBox的ID是不同的,且不是chkDel
------解决方案--------------------
设断点自己跟一下吧,呵呵
------解决方案--------------------
你在那个事件里面删除不就行了还用这么麻烦了
后台代码:

using System;
using System.Data;
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;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
//清清月儿http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa ";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
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)
{
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbo