简体   繁体   English

确认删除记录,方法

[英]Confirm delete of record, from method

How can I pop up a message box to confirm a delete, from inside a method? 如何从方法内部弹出消息框以确认删除?

Normally i would just use the following line in my button: OnClientClick="return confirm('Are you sure you want to delete this comment?');" 通常,我只在按钮中使用以下行:OnClientClick =“ return Confirm('确定要删除此评论吗?');”

However this delete method can also be called by a querystring, so I need the confirm message box functionality in the method, please? 但是,此delete方法也可以由查询字符串调用,因此,我需要该方法中的确认消息框功能吗?

// delete comment method
private void DeleteComment()
{

            int commentid = Int32.Parse(Request.QueryString["c"]);

            // call our delete method
            DB.DeleteComment(commentid);

}

I can't click the button through code, because it doesn't fire the OnClientClick event btnDelete_Click(null, null); 我无法通过代码单击按钮,因为它不会触发OnClientClick事件btnDelete_Click(null,null);

Regards 问候

Melt 熔化

你可以尝试使用这个

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

sounds like you need to check the query string with javascript and then display your prompt. 听起来您需要使用javascript检查查询字符串,然后显示提示。
Check out this post : 看看这个帖子

You could modify the method and call it in your body.onload or (jquery) .ready function. 您可以修改方法并在body.onload或(jquery).ready函数中调用它。 The problem then becomes how to let your server side method know the results? 问题就变成了如何让您的服务器端方法知道结果? You could set up a separate page that handles the delete and call it using jquery ajax post method. 您可以设置一个单独的页面来处理删除操作,并使用jquery ajax post方法调用它。

I guess that's my $.02 我猜那是我的$ .02

I would recommend doing something like this. 我建议做这样的事情。

Add a 2nd query string argument to dictate if it is confirmed or not. 添加第二个查询字符串参数来指示是否被确认。 Definitely add some code to confirm that the user is logged in so that this query string methodology doesn't get hit by a webcrawler or something and accidentally delete all your comments. 绝对添加一些代码以确认用户已登录,以使此查询字符串方法不会受到网络爬虫或其他攻击,并意外删除您的所有注释。

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM