开发者

CustomValidator on User Control not working

I've set up a CustomValidator with a ClientValidationFuncti开发者_开发百科on as:

    <asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" EnableClientScript="true" ClientValidationFunction="checkClients" ErrorMessage="You must select at least one client or staff" SetFocusOnError="true"></asp:CustomValidator>

I've set up my Javascript function as:

    <script type="text/javascript">
    function checkClients(sender, args) {
        if ($find("<%= selSelectedClientFamily.ClientID %>").get_items().get_count() > 0) {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }

</script>

I'm putting this all in a User Control which I'm dropping into a form on another page.

When testing, the validation is firing and in fact the JS function is executed and works appropriately. However - the form is submitted whether IsValid is true or false. Like it's executing but not paying attention to the result. Any ideas?


Is the page postbacked or not?

If yes, can you provide also the content of ValidateSelectedClients server-side validator?

Maybe better to check if it validates correctly on the server-side.

Other than this, if you are using your user-control more than once in the web form becareful. Because if you set the validation JS function on the markup AND if you insert more than one of that controls on the same page then you can have a problem: Because the function will have the same name, each control will override this function again and again. I suggest you to generate your client-side control on the server-side under such conditions.

To check the client-side validator: I would add alert then at the begining of client side function like:

alert( $find("<%= selSelectedClientFamily.ClientID %>").length ) 

and also

alert( $find("<%= selSelectedClientFamily.ClientID %>").get_items().length )  

Just to see if I really get the required DOM object and if I really hit that part of the code.

I would also add one last alert in the end of the code

alert('The end of client validator function is reached')


I was able to resolve this issue. Turns out there was a javascript error happening on submit with a third party control that was causing the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜