Solved

ASP.NET Button OnClientClick Confirm Dialog

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataAdapter, stored procedures and parameters 2 60
How to copy an image file into clipboard C/C++? 1 250
What are the big features of MVC5? 4 104
WPF issue with Trigger 2 146
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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