Yosief Kesete
Yosief Kesete

Reputation: 217

How to validate a custom ASP.net control before postback

I've created a custom control in a class project(DLL) and am using it in a page where I have the standard ASP.net required field validator control to ensure some property of the custom control is filled by the user.

For some reason, validation only happens after postback. I need the validation to happen on the client-side before postback i.e in a similar manner to the way a standard text box is validated by the ASP.net validator controls.

Please help.

Upvotes: 1

Views: 2473

Answers (2)

Ashish Babu
Ashish Babu

Reputation: 1175

I guess u might be wanting to fire the validation on some events like Button etc. Then check, have you made the CausesValidation Property of the Button to be true. for example

<asp:Button ID="Submit" runat="server" Text="Submit" CausesValidation="True" ClientIDMode="Static"/>

Upvotes: 1

user952072
user952072

Reputation: 107

you can use ClientValidationFunction property of CustomValidator.

  <asp:CustomValidator id="CustomValidator1"
       ControlToValidate="Text1"
       ClientValidationFunction="ClientValidate"
       OnServerValidate="ServerValidation"
       Display="Static"
       ErrorMessage="Not an even number!"          
       runat="server"/>




<script language="javascript"> 
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

Upvotes: 2

Related Questions