Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PlaceHolder vs Literal for adding HTML markup generated at runtime

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.

like image 718
Ahmed Atia Avatar asked Mar 10 '13 15:03

Ahmed Atia


1 Answers

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).

like image 113
Tim M. Avatar answered Oct 13 '22 08:10

Tim M.