C# GridView 为什么我点击删除按钮后,会先删除我选中的那一行,才弹出提示框呢?

来源:百度知道 编辑:UC知道 时间:2024/05/03 16:29:56
我的代码如下:protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
GridView1.PageIndex = 0;
//mycon = new SqlConnection(myCon);
// SqlCommand sqlcom;
bool isSelect = false;
for (int i = 0; i < GridView1.Rows.Count-1; i++)
{
bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
if (isChecked)
{
isSelect = true;
SqlConnection mycon = db.CreateConnection();
mycon.Open();
string sqlstr = "delete from information where 成果编号='" + GridView1.Rows[i].Cells[2].Text + "'";
SqlCommand mycmd = new SqlCommand(sqlstr, mycon);
mycmd.ExecuteNonQuery();
mycon.Close();
}

你写的是让他先删除在弹得 你把这个
if (!isSelect)
{
//Response.Write("<script>window.alert('请选择要删除的项')</" + "script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先选则要删除的记录!');</script>");
}
else
{
//RegisterStartupScript("alerm", "<script>window.confirm('选中删除记录吗?');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>window.confirm('你确认要删除选定的记录吗?');</script>");

放到上边

Button有个事件OnClientClick。
<asp:ImageButton id='..' onClick='..' onClientClick="return confirm('选中删除记录吗?');"></asp:ImageButton> 这样就不用在后台添加Attributes了。

if (isChecked)如果被选中就直接执行删除代码了<