Solved

ASP.NET Button OnClientClick Confirm Dialog

Posted on 2014-03-14
5
9,181 Views
Last Modified: 2014-03-19
Hello friends,

I'm looking at this example from W3Schools:  

http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_prop_webcontrol_button_onclientclick

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.

Jazon
0
Comment
Question by:piratepatrol
  • 2
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 250 total points
ID: 39932454
Hi,
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;
            }
        }
    </script>

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

0
 
LVL 3

Author Comment

by:piratepatrol
ID: 39932645
Hi techChallenger1.. Can you kindly show me the <asp:Button /> line in your aspx code?  Thanks so much.
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 39932982
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
About.aspx
Code Behind Page
About.aspx.cs
Designer Code File
About.aspx.designer.cs
0
 
LVL 27

Accepted Solution

by:
Ark earned 250 total points
ID: 39933627
Just add 'return' to the code. From your link:
was:
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?');"
0
 
LVL 3

Author Closing Comment

by:piratepatrol
ID: 39940710
Thanks, guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to have excel show file name on the title bar 4 43
Issue testing the Class Library 4 48
Problem to DocumentFormat 8 75
String manipulation in Visual Basic 7 60
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now