Public Class LiteralControl Inherits Control Implements ITextControl
Dim instance As LiteralControl
public class LiteralControl : Control, ITextControl
public ref class LiteralControl : public Control, ITextControl
public class LiteralControl extends Control implements ITextControl
ASP.NET 将所有不需要服务器端处理的 HTML 元素和可读文本编译为该类的实例。例如,在开始标记中不包含 runat="server" 属性/值对的 HTML 元素将被编译为 LiteralControl 对象。LiteralControl 对象不维护视图状态,因此必须针对每个请求重新创建 LiteralControl 对象的内容。
文本控件的行为与文本容纳器一样,这意味着可以从文本控件提取文本,并通过父服务器控件的 Controls 属性从父服务器控件的 ControlCollection 集合中移除文本控件。因此,当开发从 LiteralControl 类派生的自定义控件时,确保由控件自己执行任何所需的预处理步骤,而不是使用对 LiteralControl.Render 方法的调用来完成这些操作。通常,都会这样做以提高 Web 应用程序的响应时间。
可以以编程方式分别使用 ControlCollection.Add 或 ControlCollection.Remove 方法,从页或服务器控件添加或移除文本控件。
下面的代码示例演示如何在重写 Control.CreateChildControls 方法时使用重载 LiteralControl 构造函数。代码将两个新的 LiteralControl 对象和 TextBoxWeb 服务器控件添加到当前服务器控件的 Control.Controls 属性。
' 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
// 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>")); }
// Add two LiteralControls that render HTML H3 elements and text. protected void CreateChildControls() { this.get_Controls().Add(new LiteralControl("<h3>Value: ")); TextBox box = new TextBox(); box.set_Text("0"); this.get_Controls().Add(box); this.get_Controls().Add(new LiteralControl("</h3>")); } //CreateChildControls
Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。