ASP.NET Validation Controls and Javascript Confirm boxes

15,832

Solution 1

This seems to be a very common problem.

The workaround:

Validate the page first, then call confirm, as shown here and here. This does have the drawback of calling the validation twice - once in your code, and once in the generated code in the submit onclick.

How to make this work properly, i.e. Validate the page first (and only once), then show the confirm box, I do not yet know.

Edit: Here's a useful suggestion:

What ASP.NET does behind the scenes when validation controls exist, is add an autogenerated onClick event for each button. This OnClick event would supercede the custom OnClick event. So to overcome this I did the following:

  1. add CausesValidation = False
  2. added Validate() and IsValid code to the onClick event behind the page to simulate the now missing autogenerated validation code behind the button.

Edit 2: A complete example

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="if (Page_ClientValidate()){ return confirm('Do you want to submit this page?')}" CausesValidation="false" />

Solution 2

Confirm box in code behind after validation check

     <asp:Button ID="btnSave" runat="server" OnClientClick="javascript:return ConfirmSubmit()" OnClick="btnSave_Click" Text="Save" /> 


//---javascript -----
function ConfirmSubmit()
{
   Page_ClientValidate();
   if(Page_IsValid) {
       return confirm('Are you sure?');
    }
 return Page_IsValid;
}

Solution 3

The thing is that the Return Confirm fires prior to the validator's javascript. which all has to do with lifecycles and stuff.

If you're wanting to definitely have that behavior, what you'll need to do is change all of your validators to custom validators, roll out your own JS validation routines for the custom validators, and then call the confirm at the end of the validation routine as the final call.

if MAY change the sequence of firing, if you add the JS for the return confirm coding to the button in a HIJAX method where it's assigned to the onClick event after the page has been loaded fully into the browser--but I've never utilized that methodology for that capability, so don't quote me there.

Solution 4

can you not use the EnableClientScript property for the validator control allowing you to carry out the validation on the client side on your submit the validation will then fire??

Solution 5

The Validators are fired by a onsubmit handler on the form.

if your override form.onsubmit you'll lose the validator firing, though you may be able to manually provide the JS needed.

Share:
15,832
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a page using .NETs server-side input validation controls. This page also has a javascript confirm box that fires when the form is submitted. Currently when the Submit button is selected, the javascript confirm box appears, and once confirmed the ASP.NET server-side validation controls are fired. I would like to fire the server-side validation controls BEFORE the javascript confirm box is displayed.

    How can this be accomplished? Ive included a sample of my current code below.

    sample.aspx

    <asp:textbox id=foo runat=server />
    <asp:requiredfieldvalidator id=val runat=server controltovalidate=foo />
    <asp:button id=submit runat=server onClientClick=return confirm('Confirm this submission?') />
    

    sample.aspx.vb

    Sub Page_Load()
        If Page.IsPostback() Then
            Page.Validate()
    
            If Page.IsValid Then
                'process page here'
            End If
        End If
    End Sub
    

    Thanks for any help.

  • taylor michels
    taylor michels over 9 years
    +1 but your title is confusing. The confirm box isn't in code behind.