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.Add 或 ControlCollection.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
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>")); }
J#
// 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 安全性- AspNetHostingPermission 用于在宿主环境中进行操作。要求值:LinkDemand;权限值:Minimal。
- AspNetHostingPermission 用于在宿主环境中进行操作。要求值:InheritanceDemand;权限值:Minimal。

浙公网安备 33010602011771号