问题描述
我已经检查出一些Web窗体中的新功能4.5,我遇到了与不显眼的审定一个路障。
I've been checking out some of the new features for Web Forms in 4.5, and I have run into a road block with unobtrusive validation.
在网络不唐突的验证表单4.5取决于jQuery的,并且当其被启用,将导致在页面体服务器形式的内部的jQuery脚本参考。这听起来很棒,我很喜欢这个概念。它在我看过演示的伟大工程,并在code还原/清理是一件美好的事情。
Unobtrusive validation in Web Forms 4.5 is dependent on jQuery, and when it is enabled will result in a jQuery script reference inside of the server form in the page body. This sounds great, and I love the concept. It works great in the demos I have looked at, and the reduction/cleanup in code is a beautiful thing.
我遇到的问题,但是,当我使这个在pre-已有的项目。问题是,我有利用jQuery的为客户端交互(jQuery UI的是一个典型的例子),无数的网页和应用程序,并在这些页面我在页面的页眉部分jQuery的参考和相应的code。当启用不显眼的验证,其结果是在网页上的第二jQuery的参考,并在头断裂的JavaScript
I run into issues, however, when I enable this in pre-exisiting projects. The problem is that I have countless pages and applications that utilize jQuery for client side interactions (jQuery UI being a typical example), and in these pages I have a jQuery reference and accompanying code in the header section of the page. When unobtrusive validation is enabled, the result is a second jQuery reference on the page, and the javascript in the header breaks.
有没有为我的方法告诉脚本管理器的jQuery已经在页面加载,这样第二个引用不会被添加?另外,有我告诉脚本管理器或Web窗体框架来检查页面为现有的jQuery引用的方式?
Is there a way for the me to tell the script manager that jQuery has already been loaded in the page so that a second reference does not get added? Alternatively, is there a way for me to tell the script manager or the Web Forms framework to check the page for an existing jQuery reference?
推荐答案
不幸的是,ScriptResourceMapping是必需的。然而,一些工作可以从ScriptManager的删除引用,所以它不会再次渲染jQuery的。
Unfortunately the ScriptResourceMapping is required. However, with a bit of work you can remove the reference from the ScriptManager so it won't render jQuery again.
建立自己的类,从派生的ScriptManager在Web项目:
Create your own class that derives from ScriptManager in your web project:
using System;
using System.Linq;
using System.Web.UI;
namespace WebApplication46
{
public class CustomScriptManager : ScriptManager
{
protected override void OnInit(EventArgs e)
{
Page.PreRenderComplete += Page_PreRenderComplete;
base.OnInit(e);
}
private void Page_PreRenderComplete(object sender, EventArgs e)
{
var jqueryReferences = Scripts.Where(s => s.Name.Equals("jquery", StringComparison.OrdinalIgnoreCase)).ToList();
if (jqueryReferences.Count > 0)
{
// Remove the jquery references as we're rendering it manually in the master page <head>
foreach (var reference in jqueryReferences)
{
Scripts.Remove(reference);
}
}
}
}
}
接下来,配置在你的web.config一个tagMapping项,以便在ASP.NET会使用自定义的ScriptManager,而不是一个在箱子的:
Next, configure a tagMapping entry in your web.config so that ASP.NET will use your custom ScriptManager instead of the one in the box:
<system.web>
<pages>
<tagMapping>
<add tagType="System.Web.UI.ScriptManager" mappedTagType="WebApplication46.CustomScriptManager" />
</tagMapping>
</pages>
</system.web>
就是这样。现在你CustomScriptManager将确保所有的jQuery的引用都从自身删除的ScriptManager有机会开始它的渲染逻辑之前,你还是会满足所有的要求UnobtrusiveValidation工作(假设你在你的页面的标签的jQuery的引用)。
That's it. Now your CustomScriptManager will ensure all jQuery references are removed from itself before the ScriptManager has a chance to start its rendering logic and you'll still satisfy all the requirements for UnobtrusiveValidation to work (assuming you have a reference to jQuery in your page's tag).
这篇关于asp.net 4.5 Web窗体验证不显眼的jQuery的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!