Solved

ASP.NET Button OnClientClick Confirm Dialog

Posted on 2014-03-14
5
9,775 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 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 28

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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