将自定义验证器动态附加到控件时出现问题

时间:2011-01-25 15:37:46

标签: javascript asp.net validation telerik

任何人都知道为什么这段代码不起作用?

<script type="text/javascript">
    function BookDeskValidation(source, arguments) {
        var deskCombo = $find("<%=RadComboBoxDesk.ClientID%>");
        var bookCombo = $find("<%=RadComboBoxBook.ClientID%>");
        if (bookCombo.get_text() != "" && deskCombo.get_text() == "") {
            arguments.IsValid = false;
        }
        else {
            arguments.IsValid = true;
        }
    }

    function AttachValidator(combobox) {
        var validator = document.getElementById("<%= CustomValidatorDeskBook.ClientID %>");
        validator.controltovalidate = combobox.get_id();
        validator.ClientValidationFunction = "BookDeskValidation";
        ValidatorHookupControl(combobox, validator);
    }
</script>

<td>Book:&nbsp;</td>
        <td>
            <telerik:RadComboBox ID="RadComboBoxBook" runat="server" OnClientFocus="AttachValidator">
            </telerik:RadComboBox>
        </td>

<td width="70">Desk:&nbsp;</td>
            <td width="100">
                <telerik:RadComboBox ID="RadComboBoxDesk" runat="server" OnClientFocus="AttachValidator">
                </telerik:RadComboBox>
            </td>
<asp:CustomValidator ID="CustomValidatorDeskBook" runat="server" 
            ErrorMessage="Error" 
            ClientValidationFunction="BookDeskValidation" ForeColor="Red">*</asp:CustomValidator>

OnClientFocus很火,但是它然后似乎没有附加自定义验证器,因为当我退出应该附加验证器的控件时没有进行验证。

谢谢!

1 个答案:

答案 0 :(得分:2)

好的,我设法得到了这项工作。这就是我所做的。如果有人知道更好的方法,或者我做了什么来解决问题是不对的,请告诉我!

所以这就是我所做的:

<script type="text/javascript">
    function BookDeskValidation(source, arguments) {
        var deskCombo = $find("<%=RadComboBoxDesk.ClientID%>");
        var bookCombo = $find("<%=RadComboBoxBook.ClientID%>");
        if (bookCombo.get_text() != "" && deskCombo.get_text() == "") {
            arguments.IsValid = false;
        }
        else {
            arguments.IsValid = true;
        }
    }

    function AttachValidator(combobox) {
        var validator = document.getElementById("<%= CustomValidatorDeskBook.ClientID %>");
        validator.ControlToValidate = combobox.get_id();
    }

    function EnableValidator(combobox) {
        var validator = document.getElementById("<%= CustomValidatorDeskBook.ClientID %>");
        ValidatorEnable(validator, true);
    }
</script>

<td>Book:&nbsp;</td>
<td>
      <telerik:RadComboBox ID="RadComboBoxBook" runat="server" OnClientFocus="AttachValidator" OnClientDropDownClosed="EnableValidator">
      </telerik:RadComboBox>
</td>

基本上我摆脱了ValidatorHookupControl函数,并在事件OnClientDropDownClosed上用ValidatorEnable替换它