[英]Confirm delete of record, from method
如何从方法内部弹出消息框以确认删除?
通常,我只在按钮中使用以下行:OnClientClick =“ return Confirm('确定要删除此评论吗?');”
但是,此delete方法也可以由查询字符串调用,因此,我需要该方法中的确认消息框功能吗?
// delete comment method
private void DeleteComment()
{
int commentid = Int32.Parse(Request.QueryString["c"]);
// call our delete method
DB.DeleteComment(commentid);
}
我无法通过代码单击按钮,因为它不会触发OnClientClick事件btnDelete_Click(null,null);
问候
熔化
你可以尝试使用这个
ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete ?');");
听起来您需要使用javascript检查查询字符串,然后显示提示。
看看这个帖子 :
您可以修改方法并在body.onload或(jquery).ready函数中调用它。 问题就变成了如何让您的服务器端方法知道结果? 您可以设置一个单独的页面来处理删除操作,并使用jquery ajax post方法调用它。
我猜那是我的$ .02
我建议做这样的事情。
添加第二个查询字符串参数来指示是否被确认。 绝对添加一些代码以确认用户已登录,以使此查询字符串方法不会受到网络爬虫或其他攻击,并意外删除您的所有注释。
// delete comment method
private void DeleteComment()
{
if(Boolean.Parse(Request.QueryString["confirm"])
{
int commentid = Int32.Parse(Request.QueryString["c"]);
// call our delete method
DB.DeleteComment(commentid);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ConfirmDelete", @"
if(confirm('Are you sure you want to delete this comment?'))
window.location = '[insert delete location with confirm set to true]';
", true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.