3

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I intended the control library to possibly be used by more than just me, so I wanted to have the standard JS files in there already for them without having to re-setup every project. Is this not a standard practice?
The JS files should be related to that assembly, if not, then just plain JS files are OK I think. I saw this approach - having embedded everything in assemblies but it's extra work to work with it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.