ASP验证错误

时间:2014-03-03 03:10:48

标签: asp.net validation

我正在尝试使用验证器来确保用户提供1到99之间的名称和数字。以下是代码:

<p>
            <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="jerseyBox" ErrorMessage="Jersey number must be between 1 and 99" MaximumValue="99" MinimumValue="0"></asp:RangeValidator>
        </p>
        <p>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="jerseyBox" ErrorMessage="Must supply a jersey number"></asp:RequiredFieldValidator>
        </p>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="nameBox" ErrorMessage="Must Supply A Name"></asp:RequiredFieldValidator>

以下是我遇到的错误:

[InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).]
   System.Web.UI.ClientScriptManager.EnsureJqueryRegistered() +2178782
   System.Web.UI.WebControls.BaseValidator.RegisterUnobtrusiveScript() +10
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +9710113
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

2 个答案:

答案 0 :(得分:2)

请参阅此处http://connect.microsoft.com/VisualStudio/feedback/details/735928/in-asp-net-web-application-visual-basic-the-requiredfieldvalidator-doest-work

看起来您必须删除此行:

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
</appSettings>

或者将其更改为:

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>

将为您禁用它。

或者,您可以在Global.asax

中添加类似的内容
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition {
     Path = "~/scripts/jquery-1.4.1.min.js",
     DebugPath = "~/scripts/jquery-1.4.1.js",
     CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
     CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});

答案 1 :(得分:0)

默认情况下启用.NET 4.5 Unobtrusive Validation时。您需要在项目中使用jQuery并在 Global.asax 中使用类似的东西来正确注册jQuery:

ScriptManager.ScriptResourceMapping.AddDefinition("jquery", 
new ScriptResourceDefinition {
    Path = "~/scripts/jquery-1.4.1.min.js",
    DebugPath = "~/scripts/jquery-1.4.1.js",
    CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
    CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});

您还可以通过删除以下行在 web.config 中停用此新功能:

<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />