12

I would like to add a rendering that also has some other rendering added to its own place holder ahead of time. An example is to add a striped rendering and place a 1 column grid rendering in the striped rendering's placeholder. And you can see how I could add an image, some html, etc... And have this all added to a placeholder in one clean action.

I know that Brainjocks Score has the concept of a Snippet and not sure of what SXA calls theirs. But those are for MVC. I am looking for a little guidance on the code that would let us accomplish the same thing in webforms. Its a precompiled set of renderings that I can drop in a placeholder.

6
  • Just to make sure I understand you correctly; you're looking for sort of a composite rendering component?
    – Mark Cassidy
    Commented Oct 30, 2017 at 21:56
  • @MarkCassidy that’s exactly right. That jogs my memory that I wrote something like that in the past. I remember having it manipulate the placeholders names when it got placed. There are 10 of these available for mvc since you can use the mvc rendering pipelines. Webforms is more limited.
    – Chris Auer
    Commented Oct 30, 2017 at 22:09
  • Closest I've seen to this is this: chrisvandesteeg.nl/2015/11/03/sitecore-composite-components
    – jammykam
    Commented Oct 31, 2017 at 0:52
  • With Brainjocks SCORE, you can do this with Snippets executed as Macro Renderings. I believe SXA has a similar concept. Have not seen an open source version of this feature before.
    – jdylanmc
    Commented Oct 17, 2018 at 20:23
  • @DylanMcCurry I think this is true, but I think it is MVC only.
    – Chris Auer
    Commented Oct 18, 2018 at 3:27

2 Answers 2

1

In Sitecore Web Forms (ASPX), you can achieve a similar concept by using Sublayouts or Renderings. These are essentially controls that you can drop onto a page or within a placeholder. While SXA and Brainjocks Score provide a more structured approach, in Web Forms, you can create your own user controls or use existing ones.

Here's a simple example using a user control:

Create a user control (ASCX file) that represents your "Snippet" or precompiled rendering. You can include multiple controls within it.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MySnippet.ascx.cs" Inherits="YourNamespace.MySnippet" %>

<!-- Your HTML, controls, and logic here -->
<div>
    <h2>Striped Rendering</h2>
    <asp:PlaceHolder runat="server" ID="StripedRenderingPlaceholder"></asp:PlaceHolder>
</div>

// MySnippet.ascx.cs
public partial class MySnippet : System.Web.UI.UserControl
{
    // Code-behind logic here
}

Drop this user control onto your Sitecore layout. You can use it within a placeholder.

<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
    <uc:MySnippet ID="MySnippetControl" runat="server" />
</asp:Content>

Now, you can programmatically add additional renderings or controls to the StripedRenderingPlaceholder within your code-behind.

// In your code-behind or wherever you want to dynamically add controls
var stripedRendering = this.FindControl("MySnippetControl") as MySnippet;
if (stripedRendering != null)
{
    var gridControl = new MyGridControl();
    stripedRendering.StripedRenderingPlaceholder.Controls.Add(gridControl);
}

This approach allows you to encapsulate your "snippet" in a user control and then dynamically add other controls to it at runtime.

0

If you're in the context of a rendering control you can access its associated placeholder control through the Parent() method (you may have to use it more than once to get the placeholder). From there, you can add whatever controls you like as its children.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.