2010-06-08 86 views
0

如何从弹出的方法中弹出消息框以确认删除?确认删除记录,从方法

通常我会在我的按钮中使用以下行: OnClientClick =“return confirm('您确定要删除此评论?');”

但是这个删除方法也可以通过查询字符串来调用,所以我需要在方法中确认消息框的功能吗?

// 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);

问候

熔体

回答

0

我会推荐做这样的事情。

添加第二个查询字符串参数来决定是否确认它。绝对添加一些代码来确认用户已经登录,这样这个查询字符串方法不会被webcrawler或其他东西击中,并且意外删除所有的注释。

// 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); 
    } 

} 
1

您可以尝试使用此

ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete ?');"); 
+0

嗨,这不会导致页面加载时触发onClientClick事件。 感谢您的回复 关注 Melt – Melt 2010-06-08 16:38:32

0

听起来像你需要检查查询字符串用JavaScript,然后显示你的提示。
看看这个post

你可以修改方法,并在body.onload或(jquery).ready函数中调用它。那么问题就变成了如何让你的服务器端方法知道结果?您可以设置一个单独的页面来处理删除并使用jquery ajax post方法调用它。

我想这是我的.02