admin管理员组

文章数量:1220935

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.

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.

Share Improve this question asked Sep 26, 2008 at 14:42 MichaelMichael
Add a comment  | 

7 Answers 7

Reset to default 8

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" />

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;
}

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??

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.

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.

What about using the ASP.NET Control Toolkit's ValidatorCallout control? From: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ValidatorCallout/ValidatorCallout.aspx

ValidatorCallout is an ASP.NET AJAX extender that enhances the functionality of existing ASP.NET validators. To use this control, add an input field and a validator control as you normally would. Then add the ValidatorCallout and set its TargetControlID property to reference the validator control.

I haven't used this one, but it seems to me that it would get you the client side validation that you want.

You should validate the page on the client itself.

function validate()
{
    Page_ClientValidate();
    if (Page_IsValid)
        // do your processing here

    return Page_IsValid;
}

This method can be called on the "onClientClick" event of the button and in the code-behind, you can if the page is valid and do the processing if the client-side validation is successful.

So, on the click event of the button, you can do -

protected void SubmitButton_Click(object sender, EventArgs e)    
{    
    if (!this.isValid)
        return;

    // do the processing here
}

本文标签: netASPNET Validation Controls and Javascript Confirm boxesStack Overflow