Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class LiteralControl _
    Inherits Control _
    Implements ITextControl
Visual Basic (Usage)
Dim instance As LiteralControl
C#
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class LiteralControl : Control, 
    ITextControl
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class LiteralControl : public Control, 
    ITextControl
JScript
public class LiteralControl extends Control implements ITextControl
ASP.NET
<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.

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(box)

    Me.Controls.Add(New LiteralControl("</h3>"))
End Sub
C#
// 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(box);

    this.Controls.Add(new LiteralControl("</h3>"));
}
JScript
// Add two LiteralControls that render HTML H3 elements and text.
protected override function CreateChildControls() 
{

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

    var box : TextBox = new TextBox();
    box.Text = "0";
    this.Controls.Add(box);

    this.Controls.Add(new LiteralControl("</h3>"));
}
System..::.Object
  System.Web.UI..::.Control
    System.Web.UI..::.LiteralControl
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker