troubleshooting Question

ASP Button with Custom Validator - if True fire Javascript AND Code Behind

Avatar of alright
alrightFlag for United States of America asked on
JavaScript.NET ProgrammingASP.NET
5 Comments1 Solution1384 ViewsLast Modified:
What I want to accomplish: I have a Button and a Checkbox on a page. If the user clicks the button without first checking the box, they receive an alert box and no postback occurs.

If the checkbox is checked and the user clicks the button, the button should be immediately disabled to prevent subsequent clicks (the button sends an email - don't want it being spammed).

The issue: I have a Custom Validator set up to check if the Box is checked. This works great, no check - no post. Check - post and the Button_Click event is fired; however, this only works if I don't include the code to disable the button.

By firing the Javascript code to disable the button, only the Javascript is ran - the code-behind in the _Click event is not ran. Code below.

I've tried placing the Disable code inside an Else{} statement within the ValidateCheck() function. I've tried invoking the Click method of the Button after the Disable javascript is ran via document.getElementById("<%= btnConfirmation.ClientID %>").click();.

Whatever I seem to try I can get one part working but break another. This is rudimentary I'm sure - any help is appreciated. Thx
html:

<asp:Button ID="btnConfirmation" runat="server" Text="Confirm" onclick="btnConfirmation_Click" ValidationGroup="vgConfirmation" />


javascript:

function ValidateChecked(oSrc, args) {
            if (document.getElementById('<%= cbConfirmation.ClientID %>').checked == false) {
                alert("Check the box etc");
                args.IsValid = false;
            }


javascript i'm using to disable the button on checked click:

                var objName = '<%= btnConfirmation.ClientID %>';
                document.getElementById(objName).disabled = true;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros