PlaceHolder vs Literal for adding HTML markup generated at runtime

Ahmed Atia picture Ahmed Atia · Mar 10, 2013 · Viewed 11.9k times · Source

This question points out Literal vs Label while this question points out Panel VS. PlaceHolder but just today I was debating with my colleague on using PlacHolder vs Literal for adding HTML markup which is generated at runtime. Both controls do not produce any extra markup but we are looking for the right control for adding generated markup on the fly. The answer of this question suggests using of both for adding generated markup so I'm wondering which control/approach should we use for just adding generated markup and nothing more.

Answer

Tim Medora picture Tim Medora · Mar 10, 2013

Neither render any markup of their own (which can be a very good thing). However, a Placeholder may contain child controls, whereas a Literal cannot.

By comparison, a Placeholder can contain other controls, but does not have a Text property.

I'm wondering which control/approach should we use for just adding generated markup and nothing more.

If by "generated" you mean the end result is a string, I would use a Literal. If you are generating a control tree, then append those controls to a Placeholder.

Or, if you want to omit the declaration of a server control completely:

<h2>Hello World</h2>
<p>The following is generated markup.</p>
<%= base.GetGeneratedMarkup() %>

I believe a Literal is still generated under the hood for this, but it allows you to mix generated content with the markup portion of your page/control (similar to Razor).