1

Hi im looking to find out after I do this:

c#

div.Attributes.Add("onclick", "return confirm_delete();");

javascript:

function confirm_delete()
{
  if (confirm("Are you sure you want to delete this comment?")==true)
    return true;
  else
    return false;
}

It pops up with a message box with yes and no if I press yes how can I add a function into my c# how can I call it?

If yes go to c# code behind

and add in confirm_delete 
if yes was clicked
do this 

I need it so when some one clickes yes I can (from code behind in asp.net c#) add in a method to delete from mysql database.

Just dont know how I refrence to the javascript

public static string confirm_delete()
{

    return something (dont know how to)

}
1
  • arghhhh GOD all i want to do is have a damm message box with yes or no so I can add in a function to delete it~? WHY IS IT SO HARDD Commented Mar 28, 2011 at 19:23

4 Answers 4

1

How about this add a button with the clientside click event instead of adding it in the code behind. Check to see if the popup returns true. If you return false it should prevent the server side event from firing.

<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/* post back*/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>



protected void btnDelete_Click(object sender, EventArgs e)
{

  //run your serverside code if confirm was pressed.

}
Sign up to request clarification or add additional context in comments.

2 Comments

bweaver your method doesnt cause postback am I missing something?
Sorry, I usually do something in the client click event that causes a post back try changing the code to this: OnClientClick="return confirm_delete();"
1

Your JavaScript will need to call a page on your server. This is typically accomplished with AJAX and a web service.

This page on MSDN has a good overview of what's involved.

If you're interested in using jQuery, this page has a simple example.

3 Comments

o god, seriously. Is there a way rather than using jquery or javascript to post an asp message box instead for my div?
yes, you can do a normal postback. Look at LinkButton for the simplest procedure
Sure; just post the form if the user clicks "ok"
1

You may be able to use a PageMethod to call your codebehind function, check out the following link for an example

ASP.NET Ajax PageMethods

1 Comment

how do i add pagemethods to my own script?
0

Wondering why you are using a div? Can't you use asp.net Button / LinkButton and limiting it's postback based on confirm value? e.g.

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
    CommandName="Delete" Text="Delete"
    OnClientClick="return confirm('Are you certain you want to delete this 
product?');">
</asp:LinkButton>

Reference.

1 Comment

@Garrith: Then your next best bet will be using jQuery + ajax to do the job. You can call page method or expose your delete functionality via web service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.