我正在使用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>
你需要一张网。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>
我没有禁用新功能,而是选择按照错误提示操作。在我的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等对脚本文件的交付进行集中控制。
至少有三种方法可以禁止使用不显眼的JavaScript进行客户端验证:
将以下内容添加到web。配置文件:
< >配置
< appSettings >
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
< / appSettings >
> < /配置
设置System.Web.UI.ValidationSettings.UnobtrusiveValidationMode静态属性的值为System.Web.UI.UnobtrusiveValidationMode.None
设置System.Web.UI.Page.UnobtrusiveValidationMode实例属性的值为System.Web.UI.UnobtrusiveValidationMode.None
要禁用每个页面的功能,我更喜欢设置页面。UnobtrusiveValidationMode属性使用page指令:
<%@ Page Language="C#" UnobtrusiveValidationMode="None" %>