2011-04-15 47 views
1

如何在包含prerender事件的webcontrol中將Javascript包含到我的頁面中?包括從webcontrol代碼的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); 
    } 
} 
+0

在什麼時候添加腳本,它應該在Page_PreRender事件中,而不是Page_Load中。 – Zachary 2011-04-15 22:39:10

+0

我在WebControl的prerender事件下添加它。 – 2011-04-15 22:44:18

+0

爲什麼不使用網址作爲關鍵?網址的散列不保證唯一標識該字符串。 – Magnus 2011-04-17 08:56:44

回答

1

我想通了,控制必須位於表單中 - 指定runat =「server」。我認爲任何指定runat =「server」的控件都足夠了,但是沒有。

哇。哇哇!

0

嘗試:

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

這沒有奏效。 – 2011-04-15 22:27:32

+0

@Mathias Lykkegaard Lorenzen,當您查看生成的HTML頁面源代碼時,是否會看到腳本包含:'? – 2011-04-15 22:28:01

+0

不,這正是我沒有看到:( – 2011-04-15 22:28:22

相關問題