如何获取用户控件数据类型所在的TabPage

时间:2012-07-18 10:13:43

标签: c# user-controls tinymce umbraco

我正在使用用户控件包装器方法构建自定义数据类型。在其中我添加了现有的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正在使用递归查找控件的自定义扩展方法。

1 个答案:

答案 0 :(得分: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;
    }
}