Html WebForms UnobtrusiveValidationMode 需要“jquery”的 ScriptResourceMapping。请添加一个名为 jquery(区分大小写)的 ScriptResourceMapping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16660900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive)
提问by Teo Chuen Wei Bryan
I'm building a web application using Visual Studio 2012. I'm attempting to add word count into my textbox. However after adding the the javascript codes and the html codes. I receive the error as stated above.
我正在使用 Visual Studio 2012 构建一个 Web 应用程序。我正在尝试将字数添加到我的文本框中。但是在添加了 javascript 代码和 html 代码之后。我收到如上所述的错误。
Here is my javascript codeds
这是我的 javascript 编码
Code :
代码 :
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, "");
}
Server codes in master page
母版页中的服务器代码
<script type="text/javascript" src="js/JScript.js" ></script>
ASPX codes, ( Html codes )
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>
回答by ericdc
You need a web.config key to enable the pre 4.5 validation mode.
您需要一个 web.config 密钥来启用 pre 4.5 验证模式。
More Info on ValidationSettings:UnobtrusiveValidationMode:
有关ValidationSettings:UnobtrusiveValidationMode 的更多信息:
Specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic.
Type: UnobtrusiveValidationMode
Default value: None
Remarks: If this key value is set to "None"[default], the ASP.NET application will use the pre-4.5 behavior(JavaScript inline in the pages) for client-side validation logic. If this key value is set to "WebForms", ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script referencefor client-side validation logic.
Example:
<appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings>
指定 ASP.NET 如何全局启用内置验证器控件以将不显眼的 JavaScript 用于客户端验证逻辑。
类型:UnobtrusiveValidationMode
默认值:无
备注:如果此键值设置为“无”[默认],则 ASP.NET 应用程序将使用4.5 之前的行为(页面中的 JavaScript 内联)进行客户端验证逻辑。如果此键值设置为"WebForms",则 ASP.NET 使用HTML5 数据属性和来自客户端验证逻辑的添加脚本引用的后期绑定 JavaScript。
例子:
<appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings>
回答by b_levitt
Rather than disabling a new feature, I opted to follow the instructions of the error. In my global.asax.cs I added:
我没有禁用新功能,而是选择按照错误说明进行操作。在我的 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"
});
}
This comes from an msdn blog postwhich highlights some of the advantages of script resource mappings. Of particular interest to me was centralized control over the delivery of the script files based on "debug=true", EnableCDN, etc.
这来自msdn 博客文章,该文章强调了脚本资源映射的一些优点。我特别感兴趣的是基于“debug=true”、EnableCDN 等对脚本文件的交付进行集中控制。
回答by Ryan Prechel
There are at least three ways to disable the use of unobtrusive JavaScript for client-side validation:
至少有三种方法可以禁用不显眼的 JavaScript 进行客户端验证:
- Add the following to the web.config file:
<configuration> <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings> </configuration>
- Set the value of the
System.Web.UI.ValidationSettings.UnobtrusiveValidationMode
static property toSystem.Web.UI.UnobtrusiveValidationMode.None
- Set the value of the
System.Web.UI.Page.UnobtrusiveValidationMode
instance property toSystem.Web.UI.UnobtrusiveValidationMode.None
- 将以下内容添加到 web.config 文件中:
<configuration> <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings> </configuration>
- 将
System.Web.UI.ValidationSettings.UnobtrusiveValidationMode
静态属性的值设置为System.Web.UI.UnobtrusiveValidationMode.None
- 将
System.Web.UI.Page.UnobtrusiveValidationMode
实例属性的值设置为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.UnobtrusiveValidationMode
使用 page 指令设置属性:
<%@ Page Language="C#" UnobtrusiveValidationMode="None" %>
回答by denver
Unobtrusive validation is enabled by default in new version of ASP.NET. Unobtrusive validation aims to decrease the page size by replacing the inline JavaScript for performing validation with a small JavaScript library that uses jQuery.
新版本的 ASP.NET 中默认启用不显眼的验证。非侵入式验证旨在通过使用使用 jQuery 的小型 JavaScript 库替换用于执行验证的内联 JavaScript 来减小页面大小。
You can either disable it by editing web.config to include the following:
您可以通过编辑 web.config 以包含以下内容来禁用它:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
Or better yet properly configure it by modifying the Application_Start method in global.asax:
或者通过修改 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"
}
);
}
Page 399 of Beginning ASP.NET 4.5.1 in C# and VB provides a discussion on the benefit of unobtrusive validation and a walkthrough for configuring it.
在 C# 和 VB 中开始 ASP.NET 4.5.1 的第 399 页讨论了不显眼的验证的好处以及配置它的演练。
For those looking for RouteConfig. It is added automatically when you make a new project in visual studio to the App_Code folder. The contents look something like this:
对于那些寻找 RouteConfig 的人。当您在 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);
}
}
}
回答by BernieSF
to add a little more to the answer from b_levitt... on global.asax:
在 global.asax 上为 b_levitt ... 的答案添加更多内容:
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"
});
}
}
}
on your default.aspx
在您的 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>
回答by DaniDev
I believe I have encountered the same quandary. I started encountering the problem when I changed to:
我相信我也遇到过同样的窘境。当我改为:
</system.web>
<httpRuntime targetFramework="4.5"/>
Which gives the error message you describe above.
这给出了您在上面描述的错误消息。
adding:
添加:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
Solves the issue, but then it makes your validation controls/scripts throw Javascript runtime errors.If you change to:
解决了这个问题,但随后它会使您的验证控件/脚本抛出 Javascript 运行时错误。如果您更改为:
</system.web>
<httpRuntime targetFramework="4.0"/>
You should be OK, but you'll have to make sure the rest of your code does/ behaves as desired. You might also have to forgo some new features only available in 4.5 onward.
你应该没问题,但你必须确保你的代码的其余部分按预期做/表现。您可能还必须放弃一些仅在 4.5 及更高版本中可用的新功能。
P.S. It is highly recommended that you read the following before implementing this solution. Especially, if you use Async functionality:
PS 强烈建议您在实施此解决方案之前阅读以下内容。特别是,如果您使用 Async 功能:
https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/
https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/
UPDATE April 2017:After some some experimentation and testing I have come up with a combination that works:
2017 年 4 月更新:经过一些实验和测试后,我想出了一个有效的组合:
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
<httpRuntime targetFramework="4.5.1" />
with:
和:
jQuery version 1.11.3
jQuery 版本 1.11.3
回答by Ivan
The problem occurred due to the Control validator. Just Add the J Query reference to your web page as follows and then add the Validation Settings in your web.config file to overcome the problem. I too faced the same problem and the below gave the solution to my problem.
问题是由于控件验证器引起的。只需按如下所示将 J Query 引用添加到您的网页,然后在您的 web.config 文件中添加验证设置即可解决该问题。我也遇到了同样的问题,下面给出了我的问题的解决方案。
Step1:
第1步:
Step2 :
第2步 :
It will resolve your problem.
它将解决您的问题。
回答by Ata Hoseini
It has 3 solutions that other guys told upper... but when try Application_Start solution, My other jQuery library like Pickup_Date_and_Time doesn't work... so I test second way and it's answered: 1- set the Target FrameWork to Pre 4.5 2- Use " UnobtrusiveValidationMode="None" " in your page header =>
它有其他人告诉上层的 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" %>
it works for me and doesn't disrupt my other jQuery function.
它对我有用并且不会破坏我的其他 jQuery 函数。