我正在使用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>
至少有三种方法可以禁止使用不显眼的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" %>
在新版本的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);
}
}
}
再给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等对脚本文件的交付进行集中控制。