如何获得ASP的客户端验证。网络页面运行?

时间:2021-11-03 16:21:29

I'm trying to get client-side validation running. I've put together a very simple test - file name is aTET3.aspx:

我正在尝试运行客户端验证。我将一个非常简单的测试文件命名为aTET3.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeFile="aTET3.aspx.cs" Inherits="aTET3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TEST</title>

    <script type="text/javascript">
    //<![CDATA[
    function TEST() 
    {
        alert("INSIDE TEST");
        alert("ValidatorCommonOnSubmit()=" + ValidatorCommonOnSubmit());
        alert("Page_ClientValidate()=" + Page_ClientValidate());
    }
    //]]>
    </script>
</head>

<body link="#1A548E" vlink="#1A548E" alink="#1A548E" onunload="TEST()">

<form name="appForm" method="post" action="aTET3.aspx" id="appForm" runat="server">
<asp:ValidationSummary id="appValidationSummary" 
    ValidationGroup="appValidation" 
    DisplayMode="List"
    EnableClientScript="true"
    HeaderText="Loan application not ready"
    runat="server"
    Enabled="true"
    Visible="true" 
    ShowSummary="true" />
<asp:Label ID="lblMessage" Font-Bold="true" ForeColor="Red" runat="server" />

<br />
Enter amount:

<asp:RequiredFieldValidator ID="ApplicationAmountValidator" 
    ValidationGroup="appValidation" 
    ControlToValidate="txtApplicationAmount"
    ErrorMessage="Application amount is required." 
    EnableClientScript="true" 
    Enable="true"
    Display="Dynamic"
    runat="server">+++</asp:RequiredFieldValidator>

<asp:TextBox ID="txtApplicationAmount" Columns="6" runat="server" />

<br /><br />

<asp:Button ID="btnSave" runat="server" Text="Send Application" 
     CausesValidation="true" />

</form>


</body>
</html>

The page has a single textbox with a RequiredFieldValidator. There is also a ValidationSummary control, and a submit button. (I added the TEST() method, called on Unload, to check the state of the page just before callback.) No client-side validation occurs; instead, the request is sent back to the server. If I call Validate() on the server, then I get validation.

该页面有一个带有RequiredFieldValidator的文本框。还有一个ValidationSummary控件和一个submit按钮。(我添加了TEST()方法,在卸载时调用,在回调之前检查页面的状态。)没有出现客户端验证;相反,请求被发送回服务器。如果我在服务器上调用Validate(),就会得到验证。

I have tried adding ValidateRequest="true" to the Page directive with same results.

我尝试过在页面指令中添加ValidateRequest=“true”,结果是一样的。

When I look at the emitted Javascript, a few things jump out to me. Here's part of it:

当我看到发出的Javascript时,我突然想到了一些事情。这是它的一部分:

<script type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() {
    if (Page_ValidationActive) {
        return ValidatorCommonOnSubmit();
    }
    else {
        return true;
    }
}
// -->
</script>

Note that Page_ValidationActive is set to False, which means that ValidatorOnSubmit always returns true. That seems odd to me, except that I looked at 'WebUIValidation.js', and see that ValidatorCommonOnSubmit does not validate the page anyhow - the Page_ClientValidate() method does, but how do I get it to run?

注意Page_ValidationActive设置为False,这意味着ValidatorOnSubmit总是返回true。这对我来说似乎很奇怪,除了我看到了“WebUIValidation”。检验ValidatorCommonOnSubmit是否不会验证页面——Page_ClientValidate()方法会验证页面,但是我如何让它运行呢?

In my TEST() method, when I manually call Page_ClientValidate(), the form does get validated client-side as expected - and the postback request is sent back to the server.

在我的TEST()方法中,当我手动调用Page_ClientValidate()时,表单确实像预期的那样得到了客户端验证——回发请求被发送回服务器。

I've tested with both FireFox 3.0.10 and MSIE 7, with same results.

我已经用FireFox 3.0.10和MSIE 7进行了测试,结果是一样的。

I expect I'm missing something very basic and I'm going to end up feeling very stupid - can anyone point it out?

我想我错过了一些最基本的东西,我最终会觉得自己很愚蠢——谁能指出来吗?

1 个解决方案

#1


2  

You have to have all the validation stuff in the same validation group. Including the button that fires the validation.

您必须在同一个验证组中拥有所有的验证内容。包括触发验证的按钮。

#1


2  

You have to have all the validation stuff in the same validation group. Including the button that fires the validation.

您必须在同一个验证组中拥有所有的验证内容。包括触发验证的按钮。