?
Solved

ASP.NET Button OnClientClick Confirm Dialog

Posted on 2014-03-14
5
Medium Priority
?
9,975 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 1000 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 28

Accepted Solution

by:
Ark earned 1000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

762 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