Melt
Melt

Reputation: 501

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.DeleteComment(commentid);

}

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

Regards

Melt

Upvotes: 0

Views: 818

Answers (3)

Jonathan Park
Jonathan Park

Reputation: 775

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

}

Upvotes: 0

earthling
earthling

Reputation: 5264

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

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

you can try using this

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

Upvotes: 1

Related Questions