我正在使用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>
再给b_levitt的答案补充一点……
global.asax:
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>
我相信我也遇到过同样的困境。我开始遇到这个问题时,我改变了:
</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
你需要一张网。Config键启用4.5前验证模式。
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 reference for client-side validation
logic.
Example:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
再给b_levitt的答案补充一点……
global.asax:
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>
我没有禁用新功能,而是选择按照错误提示操作。在我的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等对脚本文件的交付进行集中控制。