Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

asp.net 4.5 Web Forms Unobtrusive Validation jQuery Issue

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.

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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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);
                }
            }
        }
    }
}

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>

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...