Embedding a Javascript resource located in another class library without ScriptManager

I am creating a new project from scratch. I have a web application in this web application. I have links created for several class libraries.

In one of these libraries, I have some common Javascript files (like jQuery) that I need to load in my web application. I can't seem to get into Javascript.

How can I access this Javascript located in another class library from my web application project?

Attached screen for better clarity.

enter image description here

Update: Is there a way to achieve this without using ScriptManager?

+3
source share
2 answers

ScriptManager, DLL, :

, .

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

TagPrefix Web.config -

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

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>

, , .

+1

, , , RegisterClientScript, WebResource.axd

"" - , Assembly.GetManifestResourceStream()

0

All Articles