Using a LiteralControl is a good solution but depending on what you need
to do, you may consider using an HtmlGenericControl instead. The primary
difference is that the LiteralControl is exactly what it sounds
like...literal. LiteralControls should be used when the control's
contents do not need any server-side manipulation beyond its creation.
With LiteralControl, the entire tag (name, attributes, and applicable
content) needs to be set to the Text property either directly or through
the constructor. If you want to dynamically change any portion of the
control, you need to rewrite the entire tag with the new value. In the
case of an IFRAME, if you wanted to dynamically changing the src attribute
requires rewriting the whole IFRAME tag.
HtmlGenericControl allows for a bit more flexibility in that you specify
the tag name either directly through the TagName property or the
constructor and you set the attributes individually through the Attributes
collection. HtmlGenericControl allows a bit more control since the tag is
only constructed when the control is rendered.
Note that the LiteralControl class is located in the System.Web.UI
namespace and the HtmlGenericControl class is located in the
System.Web.UI.HtmlControls namespace.
My preference is that I only use LiteralControl when I don't need to set
attributes. I only use LiteralControl in three scenarios:
1.) I need to write straight text
2.) I'm dynamically writing a simple tag such as STRONG or BR
[HtmlGenericControl actually renders <br></br> here]
3.) I know that I won't need to change any of the dynamic tag's
content.
In all other cases, I like to use the control that provides the closest
fit to the tag I need to render.
The needs of your application should dictate which option you choose. I
just wanted to present a possible alternative.
HTH