开发者

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?');"

However this delete method can also be called by a querystring, so I need the confirm message box functionality in the method, please?

// delete comment method
private void DeleteComment()
{

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

            // call our delete method
            DB.Delet开发者_开发百科eComment(commentid);

}

I can't click the button through code, because it doesn't fire the OnClientClick event btnDelete_Click(null, null);

Regards

Melt


you can try using this

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


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

}


sounds like you need to check the query string with javascript and then display your prompt.
Check out this post:

You could modify the method and call it in your body.onload or (jquery) .ready function. 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.

I guess that's my $.02

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜