Search code examples
c#asp.netnestedweb-partskentico

Nesting a webpart within another webpart


Does anyone know if there is a way within Kentico CMS to nest a webpart within another webpart? I did a little research and I don't see many results on the topic so it seems the short answer is no, but maybe there is a workaround? On the project I am working on it is a requirement that the content author is able to place a hamburger menu on the page and then add other content within that hamburger menu via drag and drop. How can this be achieved within Kentico?

Thanks.


Solution

  • Circling back to this post. Although adding a <cms:CMSEditableRegion /> tag to the page did indeed allow me to nest a Widget within a Webpart zone, it seemed a bit unnecessary for the user to have to switch between the design tab and page tab in order to achieve adding a nested component, not to mention having to register every webpart as a widget; quite unnecessary. I noticed Kentico's built in Layout webpart allowed for nested webparts so I looked at the code and was finally able to figure out how to implemented nesting a webpart within another webpart!

    Ensure your webpart's code-behind inherits from CMSAbstractLayoutWebPart

    Add the following method to your code-behind:

    protected override void PrepareLayout()
    {
        StartLayout();
    
        Append("<div");
        Append(" style=\"width: ", "100%", "\"");
    
        if (IsDesign)
        {
            Append(" id=\"", ShortClientID, "_env\">");
    
            Append("<table class=\"LayoutTable\" cellspacing=\"0\" style=\"width: 100%;\">");
    
            if (ViewModeIsDesign())
            {
                Append("<tr><td class=\"LayoutHeader\" colspan=\"2\">");
    
                // Add header container
                AddHeaderContainer();
    
                Append("</td></tr>");
            }
    
            Append("<tr><td id=\"", ShortClientID, "_info\" style=\"width: 100%;\">");
        }
        else
        {
            Append(">");
        }
    
        // Add the tabs
        var acc = new CMSAccordion();
        acc.ID = ID + "acc";
        AddControl(acc);
    
        if (IsDesign)
        {
            Append("</td>");
    
            if (AllowDesignMode)
            {
                // Width resizer
                Append("<td class=\"HorizontalResizer\" onmousedown=\"" + GetHorizontalResizerScript("env", "Width", false, "info") + " return false;\">&nbsp;</td>");
            }
    
            Append("</tr>");
        }
    
        // Pane headers
        string[] headers = TextHelper.EnsureLineEndings("HEADER", "\n").Split('\n');
    
        // Create new pane
        var pane = new CMSAccordionPane();
        pane.ID = ID + "pane";
    
        pane.Header = new TextTransformationTemplate(string.Empty);
        acc.Panes.Add(pane);
    
        pane.WebPartZone = AddZone(ID + "-ContentArea", ID + "-ContentArea", pane.ContentContainer);
    
        acc.SelectedIndex = 1;
    
        if (IsDesign)
        {
            if (AllowDesignMode)
            {
                Append("<tr><td class=\"LayoutFooter cms-bootstrap\" colspan=\"2\"><div class=\"LayoutFooterContent\">");
    
                // Pane actions
                Append("<div class=\"LayoutLeftActions\">");
                Append("</div></div></td></tr>");
            }
    
            Append("</table>");
        }
        Append("</div>");
    
        FinishLayout();
    }