1

How can I include Javascript onto my page from a webcontrol that is loading, under the prerender event?

Here's a function I built that I am using, but it is unfortunately not working.

/// <summary>
/// Includes a javascript on the page if it is not already included.
/// </summary>
/// <param name="url">The javascript to include on the page.</param>
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);
    }
}

I am using it like this:

ScriptHandler.IncludeJavascript("/scripts/TabControl.js");

Is that correct? Or is the path wrong? Does it need to be a server-side path?

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:

[ToolboxData("<{0}:TabControl Title=\"My tabcontrol\" runat=server>\n<{0}:TabPage Title=\"Default tab\" IsSelected=\"True\">Insert tab page content here ...</{0}:TabPage>\n<{0}:TabPage Title=\"Secondary tab\" IsSelected=\"True\">Insert tab page content here ...</{0}:TabPage>\n</{0}:TabControl>")]
[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("<div id=\"tabContainer\"><ul class=\"tabs\">");
        foreach (Control control in this.Controls)
        {
            if (control is TabPage)
            {
                (control as TabPage).RenderTitle(writer);
            }
        }
        writer.Write("</ul></div>");
        writer.Write("<div id=\"boxWithContent\">");
        foreach (Control control in this.Controls)
        {
            if (control is TabPage)
            {
                (control as TabPage).RenderControl(writer);
            }
        }
        writer.Write("</div>");
    }

    public override void RenderControl(HtmlTextWriter writer)
    {
        writer.Write("<h1>");
        writer.WriteEncodedText(this.Title);
        writer.Write("</h1>");
        base.RenderControl(writer);
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        base.RenderContents(output);
    }
}
3
  • At what point are you adding the script, it should be in the Page_PreRender event, versus Page_Load. Commented Apr 15, 2011 at 22:39
  • I am adding it under a WebControl's prerender event. Commented Apr 15, 2011 at 22:44
  • Why not use the url as the key? A hash of the url is not guaranteed to uniquely identify that string. Commented Apr 17, 2011 at 8:56

2 Answers 2

1

I figured out that the control must be located within a form-control with runat="server" specified. I thought any control with runat="server" specified would be enough, but no.

Wow. Just wow.

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

Comments

0

Try using a relative path that takes into account the virtual directory your site might be running under:

IncludeJavascript(ResolveUrl("~/scripts/TabControl.js"));

10 Comments

@Mathias Lykkegaard Lorenzen, when you look at the generated HTML page source do you see the script inclusion: <script src="/scripts/TabControl.js" type="text/javascript"></script>?
Nope, that's exactly what I'm not seeing :(
@Mathias Lykkegaard Lorenzen, where are you calling this method? I wrote a simple ASP.NET application and in the Page_Load of the default webform I called the function and the script was correctly added. The only thing I changed was MD5.GetMD5Hash(url) => var key = Encoding.ASCII.GetString(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(url))) as there is no a static GetMD5Hash method that I am aware of on the MD5 class.
I am calling it from a custom webcontrol that is loading, by overriding the OnPreRender procedure. It is being triggered. It just doesn't work.
@Mathias Lykkegaard Lorenzen, could you please show the code you are using to call this method? The exact location and method.
|

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.