3

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

  1. 客户端验证正在触发并通过警报消息正确显示失败。
  2. 验证控件从不呈现错误消息并且服务器被命中。
  3. 当服务器被击中并且验证失败时,控件会呈现正确的错误消息。

为什么客户端在验证方面工作正常,但没有呈现错误消息并继续到服务器?

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>
4

2 回答 2

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);
    }
于 2012-04-19T09:13:34.120 回答
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>

有关更多信息,请在此处查看参考 在此处输入图像描述在此处输入图像描述

于 2012-04-18T17:13:12.993 回答