Note that there are some explanatory texts on larger screens.

plurals
  1. POinclude Javascript server-side from webcontrol code?
    primarykey
    data
    text
    <p>How can I include Javascript onto my page from a webcontrol that is loading, under the prerender event?</p> <p>Here's a function I built that I am using, but it is unfortunately not working.</p> <pre><code>/// &lt;summary&gt; /// Includes a javascript on the page if it is not already included. /// &lt;/summary&gt; /// &lt;param name="url"&gt;The javascript to include on the page.&lt;/param&gt; public static void IncludeJavascript(string url) { string key = MD5.GetMD5Hash(url); ClientScriptManager manager = (HttpContext.Current.Handler as Page).ClientScript; if (!manager.IsClientScriptIncludeRegistered(manager.GetType(), key)) { manager.RegisterClientScriptInclude(manager.GetType(),key, url); } } </code></pre> <p>I am using it like this:</p> <pre><code>ScriptHandler.IncludeJavascript("/scripts/TabControl.js"); </code></pre> <p>Is that correct? Or is the path wrong? Does it need to be a server-side path?</p> <p>The full TabControl I made is this, and as you can see, I am overriding the prerender procedure and using it there. It's just not working:</p> <pre><code>[ToolboxData("&lt;{0}:TabControl Title=\"My tabcontrol\" runat=server&gt;\n&lt;{0}:TabPage Title=\"Default tab\" IsSelected=\"True\"&gt;Insert tab page content here ...&lt;/{0}:TabPage&gt;\n&lt;{0}:TabPage Title=\"Secondary tab\" IsSelected=\"True\"&gt;Insert tab page content here ...&lt;/{0}:TabPage&gt;\n&lt;/{0}:TabControl&gt;")] [ParseChildren(false)] [PersistChildren(true)] public class TabControl : WebControl { private int count; public TabControl() { count = 0; } protected override void OnPreRender(EventArgs e) { ScriptHandler.IncludeJavascript(ResolveUrl("~/scripts/TabControl.js")); base.OnPreRender(e); } [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public virtual string Title { get; set; } protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } public override string ClientID { get { return "tabSectionWrapper"; } } private TabPage selectedTab; public TabPage SelectedTab { get { return selectedTab; } set { selectedTab = value; } } protected override void RemovedControl(Control control) { if (control is TabPage) { count--; base.RemovedControl(control); } } protected override void AddedControl(Control control, int index) { if (control is TabPage) { if ((control as TabPage).IsSelected) { this.SelectedTab = control as TabPage; } (control as TabPage).ID = "" + count++; base.AddedControl(control, index); } } protected override void RenderChildren(HtmlTextWriter writer) { writer.Write("&lt;div id=\"tabContainer\"&gt;&lt;ul class=\"tabs\"&gt;"); foreach (Control control in this.Controls) { if (control is TabPage) { (control as TabPage).RenderTitle(writer); } } writer.Write("&lt;/ul&gt;&lt;/div&gt;"); writer.Write("&lt;div id=\"boxWithContent\"&gt;"); foreach (Control control in this.Controls) { if (control is TabPage) { (control as TabPage).RenderControl(writer); } } writer.Write("&lt;/div&gt;"); } public override void RenderControl(HtmlTextWriter writer) { writer.Write("&lt;h1&gt;"); writer.WriteEncodedText(this.Title); writer.Write("&lt;/h1&gt;"); base.RenderControl(writer); } protected override void RenderContents(HtmlTextWriter output) { base.RenderContents(output); } } </code></pre>
    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.
 

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