如何获取ASP.Net页的客户端验证运行? [英] How do I get Client-side validation of ASP.Net page to run?
问题描述
我试图让客户端验证的运行。我已经把一个非常简单的测试 - 文件名是aTET3.aspx:
I'm trying to get client-side validation running. I've put together a very simple test - file name is 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>
该页面有一个RequiredFieldValidator一个文本框。还有一个ValidationSummary控件,和一个提交按钮。 (我加了test()方法,叫上卸载,只是回调之前检查页面的状态。)没有客户端验证发生;相反,请求被发送回服务器。如果我调用服务器上的validate(),然后我得到验证。
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.
我曾尝试加入ValidateRequest =真与相同的结果Page指令。
I have tried adding ValidateRequest="true" to the Page directive with same results.
当我看着发出的Javascript,几件事情跳出来给我。下面是它的一部分:
When I look at the emitted Javascript, a few things jump out to me. Here's part of it:
<script type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
// -->
</script>
注意Page_ValidationActive设置为False,这意味着ValidatorOnSubmit总是返回true。这似乎很奇怪我的,只是我看着'WebUIValidation.js,并看到ValidatorCommonOnSubmit不无论如何验证页面 - 在Page_ClientValidate()方法,但我怎么得到它运行
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_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.
我既火狐3.0.10和MSIE 7的测试,以相同的结果。
I've tested with both FireFox 3.0.10 and MSIE 7, with same results.
我希望我失去了一些东西很基本的,我要结束的感觉非常愚蠢的 - 任何人都可以指出来。
I expect I'm missing something very basic and I'm going to end up feeling very stupid - can anyone point it out?
推荐答案
您必须有相同的验证组中的所有验证的东西。包括触发验证的按钮。
You have to have all the validation stuff in the same validation group. Including the button that fires the validation.
这篇关于如何获取ASP.Net页的客户端验证运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!