简体   繁体   中英

How to get TabPage on which a user control data type resides

I'm building a custom data type using the user control wrappper method. Within it I am adding the existing TinyMCE data type. The problem is that I need to find a way to dynamically get a hold of the current TabPage on which the data type resides so that I can add the TinyMCE buttons to the menu. This is what I have currently (the TabPage is hardcoded):

Using statements:

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

OnInit method:

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);
}

User control:

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

Note: Page.FindControl is using a custom extension method that recursively finds the control.

I'd love if there was a way to access the TabPage via the Umbraco API, but, after working on this for the past several hours, the only way I could get the tab was by traversing the parent controls until I came to the tab.

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;
}

Extension Methods:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM