3

我正在使用用户控件包装器方法构建自定义数据类型。在其中,我添加了现有的 TinyMCE 数据类型。问题是我需要找到一种方法来动态获取数据类型所在的当前 TabPage,以便我可以将 TinyMCE 按钮添加到菜单中。这是我目前拥有的(TabPage 是硬编码的):

使用语句:

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" />

注意: Page.FindControl 正在使用递归查找控件的自定义扩展方法。

4

1 回答 1

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;
    }
}
于 2012-07-18T12:17:58.587 回答