Passing properties into Sitecore sublayouts

I’ve run into multiple situations where I’ve wanted to reuse most of a Sitecore sublayout/control with only one or two differences in the control’s properties. For example,  this has happened when I have chunks of markup that are entirely identical on multiple pages, except for the CSS class. Using the sublayout’s parameter property, I’m able to reuse the control and set the CSS class dynamically from the sublayout’s parent page.

To do this, I pass & delimited attribute-value pairs into the sublayout’s parameter property:

From the control’s code-behind (Sub Nav.ascx.cs), I create a property to access the values I’ve passed in:

I also add a function to get the attributes’ values from the collection:

From there, it’s only a matter of invoking this method with the attribute key wherever you need the attribute value:

One last thing, this was just a quick and dirty example I wrote up from snippets of code I’ve written. Be sure to null check and set defaults where appropriate.

This entry was posted in it and tagged , , , , . Bookmark the permalink.

2 Responses to Passing properties into Sitecore sublayouts

  1. Nice idea. I would suggest to go further with it and make all your sublayouts inherit from a Base class such as BaseSublayout in which you put methods such as this in the class.

    • I'm John says:

      Thanks for bringing this up, John. Good point–it definitely makes sense to put this in a base class, especially if you use this functionality all over the place. Copying this code into each control would NOT be a good idea.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>