WebForms UnobtrusiveValidationMode需要一个脚本资源映射'jquery'请添加一个名为jquery的ScriptResourceMapping(区分大小写)

我正在使用Visual Studio 2012构建一个web应用程序。 我试图添加字数到我的文本框。 但是在添加了javascript代码和html代码之后。我收到如上所述的错误

这是我的javascript代码

代码:

function validateLimit(obj, divID, maxchar) {


objDiv = get_object(divID);


if (this.id) obj = this;


var remaningChar = maxchar - trimEnter(obj.value).length;


if (objDiv.id) {
objDiv.innerHTML = remaningChar + " characters left";
}
if (remaningChar <= 0) {
obj.value = obj.value.substring(maxchar, 0);
if (objDiv.id) {
objDiv.innerHTML = "0 characters left";
}
return false;
}
else
{ return true; }
}


function get_object(id) {
var object = null;
if (document.layers) {
object = document.layers[id];
} else if (document.all) {
object = document.all[id];
} else if (document.getElementById) {
object = document.getElementById(id);
}
return object;
}


function trimEnter(dataStr) {
return dataStr.replace(/(\r\n|\r|\n)/g, "");
}

母版页中的服务器代码

<script type="text/javascript" src="js/JScript.js" ></script>

ASPX代码,(Html代码)

<tr>
<th style="width: 595px; height: 135px;">Official Report :</th>
<td colspan="4" style="height: 135px">
<asp:TextBox ID="tbofficial" runat="server" Height="121px" TextMode="MultiLine" Width="878px" MaxLength="500"   ToolTip="Summary:(500 characters)" onkeyup="return validateLimit(this, 'lblMsg1', 500)" ></asp:TextBox>
<div id="lblMsg1">500 characters left</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="tbofficial" Display="Dynamic"
SetFocusOnError="True">*</asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
676891 次浏览

你需要一张网。Config键启用4.5前验证模式。

有关< a href = " http://msdn.microsoft.com/en-us/library/hh975440.aspx " > ValidationSettings: UnobtrusiveValidationMode < / >的更多信息:

ASP。NET全局启用内置验证器控件

. JavaScript用于客户端验证逻辑

类型:UnobtrusiveValidationMode

默认值:无

备注:如果这个键值被设置为“没有” [default], ASP。网 应用程序将使用4.5之前的行为 (JavaScript内联在 页面)用于客户端验证逻辑。如果设置了此键值 “WebForms”, ASP。NET使用HTML5数据属性和后期绑定 JavaScript从添加的脚本引用用于客户端验证 逻辑。< / p >

例子:

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

我没有禁用新功能,而是选择按照错误提示操作。在我的global.asax.cs中我添加了:

protected void Application_Start(object sender, EventArgs e)
{
string JQueryVer = "1.7.1";
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
{
Path = "~/Scripts/jquery-" + JQueryVer + ".min.js",
DebugPath = "~/Scripts/jquery-" + JQueryVer + ".js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + JQueryVer + ".min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + JQueryVer + ".js",
CdnSupportsSecureConnection = true,
LoadSuccessExpression = "window.jQuery"
});
}

这来自MSDN博客文章,它突出了脚本资源映射的一些优点。我特别感兴趣的是基于“debug=true”、EnableCDN等对脚本文件的交付进行集中控制。

在新版本的ASP.NET中,默认启用了不引人注目的验证。不引人注目的验证旨在通过使用jQuery的小型JavaScript库取代执行验证的内联JavaScript来减小页面大小。

您可以通过编辑web禁用它。配置包括以下内容:

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

或者通过修改global.asax中的Application_Start方法来正确配置它:

void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
new ScriptResourceDefinition
{
Path = "/~Scripts/jquery-2.1.1.min.js"
}
);
}

《入门ASP》399页。NET 4.5.1在c#和VB中提供了关于不显眼验证的好处的讨论和配置它的演练。

对于那些寻找routecconfig的人。当你在visual studio中创建一个新项目到App_Code文件夹时,它会自动添加。内容如下所示:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;


namespace @default
{
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
}

至少有三种方法可以禁止使用不显眼的JavaScript进行客户端验证:

  1. 将以下内容添加到web。配置文件:
    <configuration>
    <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
    </configuration>
  2. Set the value of the System.Web.UI.ValidationSettings.UnobtrusiveValidationMode static property to System.Web.UI.UnobtrusiveValidationMode.None
  3. Set the value of the System.Web.UI.Page.UnobtrusiveValidationMode instance property to System.Web.UI.UnobtrusiveValidationMode.None

To disable the functionality on a per page basis, I prefer to set the Page.UnobtrusiveValidationMode property using the page directive:

<%@ Page Language="C#" UnobtrusiveValidationMode="None" %>

为b_levitt的答案添加更多的东西… global.asax: < / p >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;


namespace LoginPage
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
string JQueryVer = "1.11.3";
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
{
Path = "~/js/jquery-" + JQueryVer + ".min.js",
DebugPath = "~/js/jquery-" + JQueryVer + ".js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + JQueryVer + ".min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + JQueryVer + ".js",
CdnSupportsSecureConnection = true,
LoadSuccessExpression = "window.jQuery"
});
}
}
}

在你的default.aspx上

<body>
<form id="UserSectionForm" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server">
<Scripts>
<asp:ScriptReference Name="jquery" />
</Scripts>
</asp:ScriptManager>
<%--rest of your markup goes here--%>
</form>
</body>

由于控制验证器而发生的问题。只需添加J查询参考到您的网页,然后添加验证设置在您的网页。配置文件来克服这个问题。我也面临着同样的问题,下面给出了解决我问题的方法。

步骤1:

Code to be added in web page

步骤2:

 Web中新增代码。配置文件 < / >

它会解决你的问题。

我相信我也遇到过同样的困境。我开始遇到这个问题时,我改变了:

</system.web>
<httpRuntime targetFramework="4.5"/>

它会给出上面描述的错误消息。

添加:

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

解决了问题,,但它会使您的验证控件/脚本抛出Javascript运行时错误。 如果更改为:

</system.web>
<httpRuntime targetFramework="4.0"/>

你应该没问题,但是,您必须确保其余的代码按照您的要求运行。您可能还必须放弃一些仅在4.5以后才可用的新功能。

附注:在实现此解决方案之前,强烈建议您阅读以下内容。特别是,如果你使用Async功能:

https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/

2017年4月更新: 经过一些实验和测试,我想出了一个组合,工作:

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
<httpRuntime targetFramework="4.5.1" />

:

jQuery 1.11.3

它有3个解,其他人告诉上… 但是当尝试Application_Start解决方案时,我的其他jQuery库如Pickup_Date_and_Time不工作… 所以我测试第二种方法,它的答案是: 1-将目标框架设置为Pre 4.5 2-在你的页面标题中使用" UnobtrusiveValidationMode="None" =>

<%@ Page Title="" Language="C#"
MasterPageFile="~/Master/MasteOfHotel.Master"
UnobtrusiveValidationMode="None" %>

它为我工作,不破坏我的其他jQuery函数。