• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10463
  • Last Modified:

ASP.NET Button OnClientClick Confirm Dialog

Hello friends,

I'm looking at this example from W3Schools:  


The problem I see here is that the server-side function is invoked whether or not the client-side function returned true.  It's not really doing a real Cancel.  How do I modify this so that the server-side function is only invoked IF the client-side function returns true.  If the client-side function returns false, then the server-side function should not be invoked.

Thanks so much for your assistance.

  • 2
  • 2
2 Solutions
Manoj PatilSr. Software EngineerCommented:
I have did it in GridView for Delete Confirmation. Check it out.

You can do this by following way..
First you need to add below code in your aspx page

<script type="text/javascript">
        function ConfirmationBox(username) {

            var result = confirm('Are you sure you want to delete ' + username + ' Details?');
            if (result) {

                return true;
            else {
                return false;

Open in new window

Next just add following code in your page load..
btnDelete.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + username + "')");

Open in new window

piratepatrolAuthor Commented:
Hi techChallenger1.. Can you kindly show me the <asp:Button /> line in your aspx code?  Thanks so much.
Manoj PatilSr. Software EngineerCommented:
Hey you don't need to call anything on Button control except Button_Click
Check below attached files which I have created for you. AND ITS WORKING EASILY
Design Page
Code Behind Page
Designer Code File
Just add 'return' to the code. From your link:
asp:Button OnClick="btClick1" OnClientClick="btClick2()"
Text="Click Me" runat="server" />
should be
asp:Button OnClick="btClick1" OnClientClick="return btClick2()"
Text="Click Me" runat="server" />

or even simply
OnClientClick="return confirm('Are you sure?');"
piratepatrolAuthor Commented:
Thanks, guys.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now