如果有一种方法可以通过 Umbraco API 访问 TabPage,我会很高兴,但是,在过去几个小时的研究之后,我获取该选项卡的唯一方法是遍历父控件,直到到达该选项卡。
Code:
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;
}
}