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开发者_StackOverflow中文版=\"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);
}
}
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.
Try using a relative path that takes into account the virtual directory your site might be running under:
IncludeJavascript(ResolveUrl("~/scripts/TabControl.js"));
精彩评论