Using JQuery as an ASP.NET embedded webresource

我与影子孤独终老i 提交于 2019-12-04 10:48:48

问题


I have an ASP.NET server control which relies on JQuery for certain functionality. I've tried to add as a webresource.

My problem is my method of including the jquery file adds it to the body, or the form to be exact:

this.Page.ClientScript.RegisterClientScriptInclude(...)

The alternative to this is to add it as a literal in the head tag:

LiteralControl include = new LiteralControl(jslink);
this.Page.Header.Controls.Add(include);

The problem with this however is any existing code srcs in the head which use JQuery fail, as JQuery is loaded afterwards (ASP.NET adds the literal at the bottom of the control tree).

Is there a practical way of making JQuery an embedded resource, but loaded in the head first? Or should I give up now.


回答1:


If you want to package up jQuery and embed it inside your own server control you should serve it to the client using the ScriptManager. From the top of my head you have to:

  1. add jQuery.js to your project
  2. under its "Build Action" Property, make it an Embedded Resource
  3. in the AssemblyInfo.cs for your control add

    [assembly: WebResource("<Your Server Control namespace>.jQuery.js", "application/x-javascript")]
    
  4. Make your control inherit from System.Web.UI.ScriptControl (or at least implement IScriptControl)

  5. Override GetScriptReferences:

    protected override IEnumerable<ScriptReference>
    GetScriptReferences()
    {
    return new ScriptReference[] { 
        new ScriptReference("<Your Server Control namespace>.jQuery.js", this.GetType().Assembly.FullName),
    };
    }
    

All of your own client script should be setup inside:

protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()

Which will then ensure the correct order of dependencies (ie jQuery will be available to your own client script).




回答2:


Update:

A far easier way of doing it is to simply add the script tag dynamically, in your script and point to the google code hosting. e.g.

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

include_dom("http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js");

The function is taken from this article


Crecentfresh pushed me in the right direction, I also found

http://en.csharp-online.net/Creating_Custom_ASP.NET_AJAX_Client_Controls—IScriptControl.GetScriptReferences_Method.

My problem still remains though, the ScriptManager adds the references after the script in the head but I think this is an issue that can't be resolved. I've opted to answer myself but also upvoted crescentfresh.



来源:https://stackoverflow.com/questions/424124/using-jquery-as-an-asp-net-embedded-webresource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!