This documentation is archived and is not being maintained.

LiteralControl Class

Represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class LiteralControl : Control, 
<asp:LiteralControl />

ASP.NET compiles all HTML elements and readable text that do not require server-side processing into instances of this class. For example, an HTML element that does not contain a runat="server" attribute/value pair in its opening tag is compiled into a LiteralControl object. LiteralControl objects do not maintain view state, so the contents of a LiteralControl object must be recreated on each request.

Literal controls behave as text holders, meaning that you can extract text from the literal control and remove the literal control from the parent server control's ControlCollection collection through the parent's Controls property. Therefore, when you develop a custom control derived from the LiteralControl class, make sure your control performs any required preprocessing steps itself, rather than using a call to the LiteralControl.Render method to accomplish them. Typically, you would do this to improve the response time of your Web application.

You can add or remove literal controls from a page or server control programmatically using the ControlCollection.Add or the ControlCollection.Remove method, respectively.

The following code example demonstrates how to use the overloaded LiteralControl constructor when overriding the Control.CreateChildControls method. The code adds two new LiteralControl objects and a TextBox Web server control to the current server control's Control.Controls property.

// Add two LiteralControls that render HTML H3 elements and text.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
protected override void CreateChildControls() {

    this.Controls.Add(new LiteralControl("<h3>Value: "));

    TextBox box = new TextBox();
    box.Text = "0";

    this.Controls.Add(new LiteralControl("</h3>"));


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0