Reputation: 53
I am trying to use validators to make sure that a user supplies a name and a number between 1 and 99. Here is the code:
<p>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="jerseyBox" ErrorMessage="Jersey number must be between 1 and 99" MaximumValue="99" MinimumValue="0"></asp:RangeValidator>
</p>
<p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="jerseyBox" ErrorMessage="Must supply a jersey number"></asp:RequiredFieldValidator>
</p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="nameBox" ErrorMessage="Must Supply A Name"></asp:RequiredFieldValidator>
Here is the error I am getting:
[InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).]
System.Web.UI.ClientScriptManager.EnsureJqueryRegistered() +2178782
System.Web.UI.WebControls.BaseValidator.RegisterUnobtrusiveScript() +10
System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +9710113
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
Upvotes: 2
Views: 2293
Reputation: 270
When targeting .NET 4.5 Unobtrusive Validation is enabled by default. You need to have jQuery in your project and have something like this in Global.asax to register jQuery properly:
ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
new ScriptResourceDefinition {
Path = "~/scripts/jquery-1.4.1.min.js",
DebugPath = "~/scripts/jquery-1.4.1.js",
CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});
You can also disable this new feature in web.config by removing the following line:
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
Upvotes: 0
Reputation: 9460
It looks like you have to either remove this line:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
</appSettings>
Or change it to this:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
Which will disable it for you.
Alternatively you could add something like this to your Global.asax
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition {
Path = "~/scripts/jquery-1.4.1.min.js",
DebugPath = "~/scripts/jquery-1.4.1.js",
CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});
Upvotes: 2