LiteralControl 类使用

.NET Framework 类库 
LiteralControl 类 

表示 HTML 元素、文本和 ASP.NET 页中不需要在服务器上处理的任何其他字符串。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

语法
Visual Basic(声明)
Public Class LiteralControl
Inherits Control
Implements ITextControl
Visual Basic(用法)
Dim instance As LiteralControl
C#
public class LiteralControl : Control, ITextControl
C++
public ref class LiteralControl : public Control, ITextControl
J#
public class LiteralControl extends Control implements ITextControl
JScript
public class LiteralControl extends Control implements ITextControl
备注

ASP.NET 将所有不需要服务器端处理的 HTML 元素和可读文本编译为该类的实例。例如,在开始标记中不包含 runat="server" 属性/值对的 HTML 元素将被编译为 LiteralControl 对象。LiteralControl 对象不维护视图状态,因此必须针对每个请求重新创建 LiteralControl 对象的内容。

文本控件的行为与文本容纳器一样,这意味着可以从文本控件提取文本,并通过父服务器控件的 Controls 属性从父服务器控件的 ControlCollection 集合中移除文本控件。因此,当开发从 LiteralControl 类派生的自定义控件时,确保由控件自己执行任何所需的预处理步骤,而不是使用对 LiteralControl.Render 方法的调用来完成这些操作。通常,都会这样做以提高 Web 应用程序的响应时间。

可以以编程方式分别使用 ControlCollection.AddControlCollection.Remove 方法,从页或服务器控件添加或移除文本控件。

示例

下面的代码示例演示如何在重写 Control.CreateChildControls 方法时使用重载 LiteralControl 构造函数。代码将两个新的 LiteralControl 对象和 TextBoxWeb 服务器控件添加到当前服务器控件的 Control.Controls 属性。

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
// 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
.NET Framework 安全性
继承层次结构
System.Object
   System.Web.UI.Control
    System.Web.UI.LiteralControl
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0
posted @ 2006-08-12 08:20  dodo-yufan  阅读(2511)  评论(0编辑  收藏  举报