3 点击“删除选中”按钮的事件。
protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0,sqlText.Length - 1) + ")";
sqlText = "delete vote where vote_id in" + sqlText;
try
{
//执行删除语句
SqlConnection conn = getCon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText,conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delCount + "条数据');</script>");
dataInit();
}
catch(Exception ex)
{
//若有错误发生,输出错误信息
Response.Write(ex.Message);
}
}
这里解释下:SQL语句删除这里使用的是: delete vote where vote_id in(1,3,5,6)
所以对于选中后,我们只需要取得(1,3,5,6)这样的语句就可以了。看上面代码,我稍微做了下注释。
文章评论
共有 位脚本之家网友发表了评论我来说两句