C# GridView 为什么我点击删除按钮后,会先删除我选中的那一行,才弹出提示框呢?
我的代码如下:protectedvoidImageButton2_Click(objectsender,ImageClickEventArgse){GridView1.P...
我的代码如下: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>");
}
DataBind();
} 展开
{
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>");
}
DataBind();
} 展开
4个回答
展开全部
你写的是让他先删除在弹得 你把这个
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>");
放到上边
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了。
<asp:ImageButton id='..' onClick='..' onClientClick="return confirm('选中删除记录吗?');"></asp:ImageButton> 这样就不用在后台添加Attributes了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if (isChecked)如果被选中就直接执行删除代码了
这里没判断就直接删除了,没有<script>提示
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;
}
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>");
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();
}
}
DataBind();
}
这里没判断就直接删除了,没有<script>提示
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;
}
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>");
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();
}
}
DataBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
逻辑不对呗!
我不知道楼上说的对错!反正我觉得,你调试一下就可以解决的!(F11)
我不知道楼上说的对错!反正我觉得,你调试一下就可以解决的!(F11)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询