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.
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).
PlaceHolder vs Literal for adding HTML
The main different is the the Literal control have the Text
property (and the Mode) that is also saved on the Viewstate, the PlaceHolder
is totally empty.
So you can use a Literal control to add direct some string html code, and you can use the PlaceHolder to add some other controls but not adding any direct html code.
© 2022 - 2024 — McMap. All rights reserved.