从webcontrol代码包含Javascript服务器端?

时间:2011-04-15 22:21:27

标签: c# javascript asp.net

如何在prerender事件下从正在加载的web控件中将Javascript包含到我的页面中?

这是我正在使用的一个功能,但遗憾的是它无法正常工作。

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

我正在使用它:

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

这是对的吗?或路径错了?它需要是服务器端路径吗?

我制作的完整TabControl就是这个,正如你所看到的,我正在重写prerender程序并在那里使用它。它只是不起作用:

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

2 个答案:

答案 0 :(得分:1)

我发现控件必须位于使用runat =&#34; server&#34;的表单控件中。指定。我认为任何控制与runat =&#34;服务器&#34;指定就够了,但没有。

哇。哇。

答案 1 :(得分:0)

尝试使用考虑您的网站可能运行的虚拟目录的相对路径:

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