【问题标题】:Confirm delete of record, from method确认删除记录,从方法
【发布时间】:2010-06-08 16:25:10
【问题描述】:

如何从方法内部弹出一个消息框来确认删除?

通常我会在我的按钮中使用以下行: 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);

问候

融化

【问题讨论】:

    标签: c# javascript .net asp.net


    【解决方案1】:

    你可以试试这个

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

    【讨论】:

    • 您好,这不会在页面加载时触发 onClientClick 事件。感谢您的回复问候融化
    【解决方案2】:

    我建议做这样的事情。

    添加第二个查询字符串参数来指示它是否被确认。一定要添加一些代码来确认用户已登录,这样这个查询字符串方法就不会被网络爬虫或其他东西击中并意外删除你所有的 cmets。

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

    【讨论】:

      【解决方案3】:

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

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

      我猜那是我的 $.02

      【讨论】:

        猜你喜欢
        • 2014-09-11
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 2022-07-26
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        • 2011-08-16
        相关资源
        最近更新 更多