Search code examples
c#asp.netsetup-project

Embedding Javascript resource located in another class library without ScriptManager


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 that Javascript, located in a different class library, from my web application project?

Attached is a screen shot for better clarity.

enter image description here

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


Solution

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