0

我在 Windows 10 上使用 Visual Studio 2012。当我在 Web 表单中使用验证时,它显示已添加验证(在设计视图中),但是当我在浏览器中打开时它不显示任何内容。帮助

<%@ Page Language="C#" %>
<script runat="server">

    void Button1_Click(Object sender, EventArgs e) {
       Label1.Text = "VALID NUMBER!";
    }

</script>
<html>
<head>
    <script language="JavaScript">
        function validateNumber(oSrc, args) {
           args.IsValid = (args.Value % 5 == 0);
        }
    </script>
</head>
<body>
    <form runat="server">
        <p>
            Number: 
            <asp:TextBox id="TextBox1" 
             runat="server"></asp:TextBox>
             &nbsp;
            <asp:CustomValidator id="CustomValidator1" 
             runat="server" ControlToValidate="TextBox1" 
             ErrorMessage="Number must be divisible by 5" 
             ClientValidationFunction="validateNumber">
            </asp:CustomValidator>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" 
             runat="server" Text="Button"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>

4

1 回答 1

0

您必须定义 的Text属性CustomValidator才能在验证失败时看到标记:

<asp:CustomValidator id="CustomValidator1" runat="server" Text="*" ... />

为了查看错误消息,您应该ValidationSummary在表单中添加一个:

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" />

最后,在您的validateNumber函数中,您应该Value在测试之前将参数转换为数字:

var value = parseInt(args.Value);
于 2016-04-01T21:55:38.950 回答