更新:2007 年 11 月
命名空间:
System.Web.UI.WebControls 程序集:
System.Web(在 System.Web.dll 中)
<ControlValuePropertyAttribute("Text")> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Label _
Inherits WebControl _
Implements ITextControl
[ControlValuePropertyAttribute("Text")]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Label : WebControl, ITextControl
[ControlValuePropertyAttribute(L"Text")]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class Label : public WebControl,
ITextControl
/** @attribute ControlValuePropertyAttribute("Text") */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class Label extends WebControl implements ITextControl
public class Label extends WebControl implements ITextControl
使用 Label 控件在网页的设置位置上显示文本。不像静态文本,可以通过 Text 属性自定义显示文本。
也可以使用 Literal 和 PlaceHolder 控件在 Web 窗体页上显示文本。但与 Label 控件不同的是,这些控件不会呈现任何附加标记。
安全说明: |
|---|
此控件可用于显示用户输入,这可能会导致安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。 |
辅助功能
默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能。
下面的示例说明如何在网页上创建 Label 控件。
说明: |
|---|
下面的代码示例使用单文件代码模型,如果将它直接复制到代码隐藏文件中,可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 ASP.NET 网页代码模型的更多信息,请参见 ASP.NET 网页代码模型。 |
安全说明: |
|---|
此示例有一个接受用户输入的文本框,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。 |
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>Label Example</title>
<script language="VB" runat="server">
Sub Button_Click(Sender As Object, e As EventArgs)
Label1.Text = Server.HtmlEncode(Text1.Text)
End Sub
</script>
</head>
<body>
<form id="Form1" runat="server">
<h3>Label Example</h3>
<asp:Label id="Label1"
Text="Label Control"
runat="server"/>
<p>
<asp:TextBox id="Text1"
Text="Copy this text to the label"
Width="200px"
runat="server" />
<asp:Button id="Button1"
Text="Copy"
OnClick="Button_Click"
runat="server"/>
</p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>Label Example</title>
<script language="C#" runat="server">
void Button_Click(Object Sender, EventArgs e)
{
Label1.Text = Server.HtmlEncode(Text1.Text);
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<h3>Label Example</h3>
<asp:Label id="Label1"
Text="Label Control"
runat="server"/>
<p>
<asp:TextBox id="Text1"
Text="Copy this text to the label"
Width="200px"
runat="server" />
<asp:Button id="Button1"
Text="Copy"
OnClick="Button_Click"
runat="server"/>
</p>
</form>
</body>
</html>
System..::.Object
System.Web.UI..::.Control
System.Web.UI.WebControls..::.WebControl
System.Web.UI.WebControls..::.Label
System.Web.UI.WebControls..::.BaseValidator
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
参考
其他资源