Search code examples
jqueryasp.netclient-side-validation

Call custom client side validation function after WebForm_OnSubmit? .NET


I want to run a simple JS function after my .NET validator controls run their javascript. My onsubmit value is javascript:return WebForm_OnSubmit();, which seems to be generated automatically and I can't change it.

Am I right in assuming I need to just add a function in this field to get it to work? Also, my form is validating after a field has been changed (not once the form has been submitted), which is great, but is there a setting soimewhere to turn that on/off?

Thanks!!


Solution

  • Althought it is a late answer I will post for future.
    If your aspx page is a simple page (no ContentPage, so no MasterPage) you can add onsubmit function directly on the markup:

    <form ... onsubmit="return myCustomValidation();"... >
    

    "WebForm_OnSubmit()" will be executed first and then if validation is ok "myCustomValidation()" will be executed.

    If your aspx page is a ContentPage (so it has a MasterPage) you don't have access to markup in order to do the same as before. You can set "myCustomValidation()" to be executed after "WebForm_OnSubmit()" in code behind:

    Private Sub Page_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete
        If Not Page.ClientScript.IsOnSubmitStatementRegistered(Me.GetType, "aspxMyCustomValidation") Then
            Page.ClientScript.RegisterOnSubmitStatement(Me.GetType, "aspxMyCustomValidation", "myCustomValidation()")
        End If
    End Sub
    

    The key is to set this code in "PreRenderComplete" event and not in "Load" event, in order to execute "myCustomValidation()" after "WebForm_OnSubmit()".