CustomValidator在客户端上触发,但验证失败时验证控件不会呈现

时间:2012-04-18 15:52:34

标签: asp.net

我有一个简单的方法:如果选中'是'单选按钮,则会使文本框成为必需的情况。我提供了客户端和服务器端验证。我发现的是:

  1. 客户端验证通过警报消息正在触发并正确显示故障。
  2. 验证控件永远不会呈现错误消息,服务器也会被命中。
  3. 当服务器命中并且验证失败时,控件会显示正确的错误消息。
  4. 为什么客户端在验证方面正常工作,但没有呈现错误消息并继续服务器?

    asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script> 
    
    <script type="text/javascript">
        function testClientValidation(src, args) {
            if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
                if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
                    args.isValid = !($('#<%=txtTest.ClientID%>').val() == "");  
                } else {
                    args.isValid = true;
                }
            } else {
                args.isValid = true;
            }
    
            alert(args.isValid);
        }
    </script>
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
    <asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow" runat="server">
        <asp:ListItem>Yes</asp:ListItem>
        <asp:ListItem>No</asp:ListItem>
    </asp:RadioButtonList> 
    
    <asp:TextBox ID="txtTest" runat="server"/>
    
    <asp:CustomValidator ID="cust" ControlToValidate="rblstTest" 
                                    OnServerValidate="testSeverValidation" 
                                    ClientValidationFunction="testClientValidation" 
                                    Display="Dynamic" 
                                    ErrorMessage="Error!" runat="server"/>
    
     <asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit"  />
     </div>  
     </asp:Content>
    

2 个答案:

答案 0 :(得分:4)

使用args.IsValid代替args.isValid

function testClientValidation(src, args) {
        if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
            if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
                args.IsValid = !($('#<%=txtTest.ClientID%>').val() == "");  
            } else {
                args.IsValid = true;
            }
        } else {
            args.IsValid = true;
        }

        alert(args.IsValid);
    }

答案 1 :(得分:0)

我添加了Hidden Field新添加的内容。

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">

    function testClientValidation(src, args) {
        debugger;
        if (document.getElementById('<%= hdn.ClientID %>').value == '1') {
            var txtBox = document.getElementById('<%= txtTest.ClientID%>');
            if (txtBox.value == '') {
                document.getElementById('cust').style.display = 'block';
                document.getElementById('cust').innerHTML = 'Error!'
                document.getElementById('<%= hdn.ClientID %>').value = "1";
                args.IsValid = false;
                return false;
            }
            else {
                document.getElementById('cust').style.display = 'none';
                document.getElementById('cust').innerHTML = '';
                document.getElementById('<%= hdn.ClientID %>').value = "0";
                args.IsValid = true;
            }
        }
        else {
            document.getElementById('cust').style.display = 'none';
            document.getElementById('cust').innerHTML = '';
            document.getElementById('<%= hdn.ClientID %>').value = "0";
            args.IsValid = true;
        }
        return true;
    }


    $(document).ready(function () {
        $("span input[type='radio']").click(function () {
            debugger;
            if ($(this).val() == 'Yes') {
                document.getElementById('<%= hdn.ClientID %>').value = "1";
                if (document.getElementById('<%= txtTest.ClientID%>').value == '') {
                    Page_IsValid = false;
                    document.getElementById('<%= hdn.ClientID %>').value = "1";
                }
                else {
                    Page_IsValid = true;
                }
            }
            else {
                document.getElementById('<%= hdn.ClientID %>').value = "0";
                Page_IsValid = true;
            }
            return true;
        });
    });
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow"
            runat="server">
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
        <asp:TextBox ID="txtTest" runat="server" />
        <asp:CustomValidator ClientIDMode="Static" ID="cust" OnServerValidate="testSeverValidation"
            ClientValidationFunction="testClientValidation" Display="Dynamic" ErrorMessage="Error!"
            runat="server" />
        <asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit" />
        <asp:HiddenField ID="hdn" runat="server" Value="0" />
    </div>
</asp:Content>

有关详细信息,请参阅参考 here enter image description here enter image description here