string ID=null; foreach(DataGridItem item in DataGrid1.Items) { CheckBox txtID = (CheckBox)item.FindControl("chkDelServer"); if (txtID.Checked == true) {
ID=txtID.Text; string SQL = "DELETE FROM News_News WHERE News_ID="+ ID +""; string connstr=System.Configuration.ConfigurationSettings.AppSettings["strConnection"].ToString();
附:
我刚写的实现删除的代码
private void Button6_Click(object sender, System.EventArgs e)
{
string ID=null;
foreach(DataGridItem item in DataGrid1.Items)
{ CheckBox txtID = (CheckBox)item.FindControl("chkDelServer"); if (txtID.Checked == true)
{
ID=txtID.Text;
string SQL = "DELETE FROM News_News WHERE News_ID="+ ID +"";
string connstr=System.Configuration.ConfigurationSettings.AppSettings["strConnection"].ToString();
SqlConnection conn=new SqlConnection(connstr);
SqlCommand comm=new SqlCommand(SQL,conn); conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
} Response.Redirect("xxxx.aspx");//返回}
{
string dgIDs = "";
bool BxsChkd = false;
foreach (DataGridItem i in MyDataGrid.Items)
{
CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");
if (deleteChkBxItem.Checked)
{
BxsChkd = true;
dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";
}
}
string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")"; if (BxsChkd == true)
{
try
{
//SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);//根据delete from youtable where id in()来执行批删除
OutputMsg.InnerHtml+="删除"+dgIDs.ToString().Trim()+"成功";
OutputMsg.Style["color"] = "#3366cc";
}
catch (SqlException err)
{
OutputMsg.InnerHtml += err.Message.ToString();
OutputMsg.Style["color"] = "#6699cc";
}
BindData();
}
http://www.cnblogs.com/smilnet/archive/2004/07/17/25150.aspx