开发者

Embedding Javascript resource located in another class library without ScriptManager

开发者 https://www.devze.com 2023-02-17 22:34 出处:网络
I am creating a new project from the ground up.I have my web application in this web application I have references set up to multiple class libraries.

I am creating a new project from the ground up. I have my web application in this web application I have references set up to multiple class libraries.

In one of those libraries I have some general Javascript files (like jQuery) that I need to load in my web application's mastersheet. I cannot seem to get at the Javascript.

How can I access th开发者_Python百科at Javascript, located in a different class library, from my web application project?

Attached is a screen shot for better clarity.

Embedding Javascript resource located in another class library without ScriptManager

Update: Is there any way do achieve this without using ScriptManager?


If you are looking to get out of using the ScriptManager but still retain the ability to store libraries in your DLL, try the following:

  1. Make a custom control in the assembly where your javascript library resides.
  2. Use ClientScriptManager.GetWebResourceUrl()
  3. Add the control the head of your master page (or anywhere else you like)

I've include a short example of how to achieve this.

Custom Control in Mri.Controls

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;

namespace Mri.Controls
{
    public class ScriptLoader : Control
    {
        protected List<string> ScriptUrls;

        public ScriptLoader()
        {
            ScriptUrls = new List<string>();
        }

        // Have to add libraries here because cannot access the Page object from the Constructor
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            AddScriptKey("Mri.Controls.Resources.Scripts.Libraries.jQuery.js");
        }

        public void AddScriptKey(string key)
        {
            //  Using the assembly location, find the WebResourceUrl
            var webResourceUrl = Page.ClientScript.GetWebResourceUrl(typeof(ScriptLoader), key);
            AddScriptUrl(webResourceUrl);
        }

        public void AddScriptUrl(string url)
        {
            //  Check to see if script already exists
            if (!ScriptUrls.Any(s => s.Equals(url)))
                ScriptUrls.Add(url);
        }

        protected override void Render(HtmlTextWriter writer)
        {    
            //  Render the script tags
            foreach (var scriptUrl in ScriptUrls)
            {
                writer.Write(string.Format("\n<script type=\"text/javascript\" src=\"{0}\"></script>", scriptUrl));
            }
        }
    }
}

Add a TagPrefix to your Web.config in your Web Application

<pages>
    <controls>
        <add tagPrefix="mri" namespace="Mri.Controls" assembly="Mri.Controls"/>
    </controls>
  </pages>

Sample Mastersheet

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page Title</title>
    <mri:ScriptLoader id="scriptLoader" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="cphBody" runat="server" />
    </form>
</body>
</html>

I hope this was along the lines of what you were looking for.


Usually resources embedded in assembly are used by controls within this assembly, registered via RegisterClientScript and then provided to client with WebResource.axd handler

if you want to 'extract' the resource from assembly and do something with it, use Assembly.GetManifestResourceStream()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号