asp.net 4.5 Web Forms Unobtrusive Validation jQuery 问题 [英] asp.net 4.5 Web Forms Unobtrusive Validation jQuery Issue

查看:24
本文介绍了asp.net 4.5 Web Forms Unobtrusive Validation jQuery 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看 4.5 中 Web 表单的一些新功能,但在不引人注目的验证方面遇到了障碍.

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.

Web Forms 4.5 中的非侵入式验证依赖于 jQuery,当它启用时,将在页面正文中的服务器表单内产生一个 jQuery 脚本引用.这听起来很棒,我喜欢这个概念.它在我看过的演示中效果很好,代码中的减少/清理是一件很棒的事情.

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.

但是,当我在预先存在的项目中启用此功能时,我遇到了问题.问题是我有无数的页面和应用程序使用 jQuery 进行客户端交互(jQuery UI 是一个典型的例子),在这些页面中,我在页面的标题部分有一个 jQuery 参考和随附的代码.当启用非侵入式验证时,结果是页面上的第二个 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 的类:

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 将确保在 ScriptManager 有机会启动其呈现逻辑之前从其自身中删除所有 jQuery 引用,并且您仍将满足 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 Forms Unobtrusive Validation jQuery 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆