Getting the value returned from a confirmation box using c#

I have this javascript within my aspx page and i want to
be able to determine my users response. if they click ok
on the confirmation message i want to run a set of C# code
and if they click cancel I dont want anything to happen.

How can i store the result of
btnExit.Attributes.Add("onclick", "return confirm_delete();");

below in a variable. I will like to determine that value of true
or false stored to determine which code to run.

I am using 2005 c#


<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">      
        function confirm_delete()
            if (confirm("The checked file(s) will be deleted?")==true)
                  return true;
                  return false;
    <form id="form1" runat="server">
        <asp:Button ID="btnExit" runat="server" Height="53px" OnClick="Button1_Click" Text="Button"
            Width="198px" /></div>
protected void Button1_Click(object sender, EventArgs e)
        String result;
        btnExit.Attributes.Add("onclick", "return confirm_delete();");
	// if confirm_delete() = true
        // dosomething
	// else
	// do nothing

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First of all, you need to put the code that adds the attribute into your Page_load sub so that the attribute is set when the page is sent to the client.  That enables the javascript to act when the button is pressed.

Second, confirm() will return true or false.  Then, the OnClick event for your C# code will only be called if  the Confirm() returns true.

This example uses the built-in java confirm function to pop up a confirmation window and wait for input
        btnExit.Attributes.Add("onclick", "javascript:if(confirm('Delete this record?')== false) return false;")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SirdotsAuthor Commented:
Thanks. I added the attribute code to my page load and it works well. Once I click ok it processes code within my click event and once I click cancel it doesnt do any thing which makes it good.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.