Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It doesn't look like there's a simple option 'built-in' to the ASP.NET MVC framework just yet. If you are using a user control (.ascx), which you may be if you are creating self-contained controls which also want to manage their own JavaScript requirements, then you can't even use the placeholders to help you out.</p> <p>In the end I created a helper class and in it there are a couple of methods:</p> <pre><code>private static SortedList&lt;int, string&gt; GetRegisteredScriptIncludes() { var registeredScriptIncludes = System.Web.HttpContext.Current.Items["RegisteredScriptIncludes"] as SortedList&lt;int, string&gt;; if (registeredScriptIncludes == null) { registeredScriptIncludes = new SortedList&lt;int, string&gt;(); System.Web.HttpContext.Current.Items["RegisteredScriptIncludes"] = registeredScriptIncludes; } return registeredScriptIncludes; } public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script) { var registeredScriptIncludes = GetRegisteredScriptIncludes(); if (!registeredScriptIncludes.ContainsValue(script)) { registeredScriptIncludes.Add(registeredScriptIncludes.Count, script); } } public static string RenderScripts(this HtmlHelper htmlhelper) { var registeredScriptIncludes = GetRegisteredScriptIncludes(); var scripts = new StringBuilder(); foreach (string script in registeredScriptIncludes.Values) { scripts.AppendLine("&lt;script src='" + script + "' type='text/javascript'&gt;&lt;/script&gt;"); } return scripts.ToString(); } </code></pre> <p>That's a basic form of it anyway to try and show the way it works. It could be enhanced in many ways, but at the moment it just filters out duplicate script insert requests for you. Whenever you want to add a new script in the ascx (or aspx for that matter) you can do it this way:</p> <pre><code>&lt;% Html.RegisterScriptInclude(Url.Content("~/Scripts/MapLayers/MapLayer.js")); Html.RegisterScriptInclude(Url.Content("~/Scripts/MapLayers/Vehicles.js")); %&gt; </code></pre> <p>Then you need to remember to output them once you're done. This is achieved by making the following call at the place in your page where you want to output the script tags:</p> <pre><code>&lt;%=Html.RenderScripts() %&gt; </code></pre> <p>Seems to work so far for me. I did half expect to have rendering issues depending at what point RenderScripts was called, especially if not all of the RegisterScriptIncludes had been called yet, but so far it seems to do the job. If you render the scripts last then you should have no problems.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload