这是我点击后的删除功能.谁能告诉我如何做一个简单的确认功能?
ASP.net C#.
以前我有这个
ScriptManager.RegisterStartupScript(this,
this.GetType(),
"script",
"confirm('Are you sure you want to Delete Your Discussion?');",
true);
但上述代码在删除后运行.
protected void lnk_delete_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string fieldID = grdrow.Cells[0].Text;
string query = "Delete from Comment where DiscussionID=@id";
SqlCommand cmd = new SqlCommand(query, cn);
cmd.Parameters.AddWithValue("@id", fieldID);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
string query1 = "Delete from Discussion where DIscussionID=@id";
SqlCommand cmd1 = new SqlCommand(query1, cn);
cmd1.Parameters.AddWithValue("@id", fieldID);
cn.Open();
cmd1.ExecuteNonQuery();
cn.Close();
GridView1.DataBind();
}
解决方法:
我想这取决于你放置RegisterStartupScript代码块的位置.理想情况下,您希望在客户端而不是服务器端进行单击确认.
因此,在用户单击以删除的按钮上,您需要一个客户端点击处理程序(如果它是服务器端按钮,事件是onClientClick),它调用一个返回confirm()调用结果的函数.像这样的东西:
<asp:Button ID="lnk_delete" runat="server" onClientClick="return fnConfirmDelete();" onClick="lnk_delete_Click">Delete</asp:Button>
<script language="javascript" type="text/javascript">
function fnConfirmDelete() {
return confirm("Are you sure you want to delete this?");
}
</script>
使用两个return语句可确保如果用户按下取消,则提交不会继续(即取消按钮单击).