2012-07-18 55 views
3

我正在使用用戶控件包裝器方法構建自定義數據類型。在它內部,我添加了現有的TinyMCE數據類型。問題是我需要找到一種方法來動態獲取數據類型所在的當前TabPage,以便我可以將TinyMCE按鈕添加到菜單中。這是我目前(在TabPage的是硬編碼):如何獲取用戶控件數據類型所在的TabPage

using語句:

using umbraco.cms.businesslogic.datatype; 
using umbraco.editorControls.tinyMCE3; 
using umbraco.uicontrols; 

OnInit方法:

private TinyMCE _tinymce = null; 

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 

    this.ID = "crte"; 

    DataTypeDefinition d = DataTypeDefinition.GetDataTypeDefinition(-87); 
    _tinymce = d.DataType.DataEditor as TinyMCE; 
    ConditionalRTEControls.Controls.Add(_tinymce); 

    TabView tabView = Page.FindControl("TabView1", true) as TabView; 
    TabPage tabPage = tabView.Controls[0] as TabPage; 
    tabPage.Menu.InsertSplitter(); 
    tabPage.Menu.NewElement("div", "umbTinymceMenu_" + _tinymce.ClientID, "tinymceMenuBar", 0); 
} 

用戶控制:

<asp:PlaceHolder ID="ConditionalRTEControls" runat="server" /> 

不是e: Page.FindControl使用遞歸查找控件的自定義擴展方法。

回答

1

如果有一種方法可以通過Umbraco API訪問TabPage,但是在過去的幾個小時內完成這項工作之後,我唯一可以得到該選項卡的方法是遍歷父控件,直到我來到選項卡。

代碼:

private TinyMCE _tinymce = null; 

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 

    this.ID = "crte"; 

    DataTypeDefinition d = DataTypeDefinition.GetDataTypeDefinition(-87); 
    _tinymce = d.DataType.DataEditor as TinyMCE; 
    ConditionalRTEControls.Controls.Add(_tinymce); 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    TabView tabView = Page.FindControl("TabView1", true) as TabView; 
    TabPage tabPage = GetCurrentTab(ConditionalRTEControls, tabView); 
    tabPage.Menu.NewElement("div", "umbTinymceMenu_" + _tinymce.ClientID, "tinymceMenuBar", 0); 
} 

private TabPage GetCurrentTab(Control control, TabView tabView) 
{ 
    return control.FindAncestor(c => tabView.Controls.Cast<Control>().Any(t => t.ID == c.ID)) as TabPage; 
} 

擴展方法:

public static class Extensions 
{ 
    public static Control FindControl(this Page page, string id, bool recursive) 
    { 
     return ((Control)page).FindControl(id, recursive); 
    } 

    public static Control FindControl(this Control control, string id, bool recursive) 
    { 
     if (recursive) 
     { 
      if (control.ID == id) 
       return control; 

      foreach (Control ctl in control.Controls) 
      { 
       Control found = ctl.FindControl(id, recursive); 
       if (found != null) 
        return found; 
      } 
      return null; 
     } 
     else 
     { 
      return control.FindControl(id); 
     } 
    } 

    public static Control FindAncestor(this Control control, Func<Control, bool> predicate) 
    { 
     if (predicate(control)) 
      return control; 

     if (control.Parent != null) 
      return control.Parent.FindAncestor(predicate); 

     return null; 
    } 
} 
相關問題