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.

For a list of all members of this type, see LiteralControl Members.


[Visual Basic]
Public Class LiteralControl
   Inherits Control
public class LiteralControl : Control
public __gc class LiteralControl : public Control
public class LiteralControl extends Control

Thread Safety

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


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.

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 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 call 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 ControlCollection.Remove method, respectively.


The following 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.

[Visual Basic] 
' Add two LiteralControls that render HTML H3 elements and text.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub CreateChildControls()

    Me.Controls.Add(New LiteralControl("<h3>Value: "))

    Dim Box As New TextBox
    Box.Text = "0"

    Me.Controls.Add(New LiteralControl("</h3>"))
End Sub

// 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>"));

// Add two LiteralControls that render HTML H3 elements and text.
[System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] 
void CreateChildControls() 
   this->Controls->Add(new LiteralControl(S"<h3>Value: "));

   TextBox* box = new TextBox();
   box->Text = S"0";

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



Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

LiteralControl Members | System.Web.UI Namespace | Control