表示在网页上显示文本的标签控件。
命名空间:System.Web.UI.WebControls 程序集:System.Web(在 system.web.dll 中)
语法
Visual Basic(声明)
<ControlValuePropertyAttribute("Text")> _ Public Class Label Inherits WebControl Implements ITextControl
Visual Basic(用法)
Dim instance As Label
C#
[ControlValuePropertyAttribute("Text")] public class Label : WebControl, ITextControl
C++
[ControlValuePropertyAttribute(L"Text")] public ref class Label : public WebControl, ITextControl
J#
/** @attribute ControlValuePropertyAttribute("Text") */ public class Label extends WebControl implements ITextControl
JScript
ControlValuePropertyAttribute("Text") public class Label extends WebControl implements ITextControl
备注
使用 Label 控件在网页的设置位置上显示文本。不像静态文本,可以通过 Text 属性自定义显示文本。
也可以使用 Literal 和 PlaceHolder 控件在 Web 窗体页上显示文本。但与 Label 控件不同的是,这些控件不会呈现任何附加标记。
警告 |
---|
此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全、如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入 和 在 ASP.NET 网页中验证用户输入。 |
辅助功能
默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能。
示例
下面的示例说明如何在网页上创建 Label 控件。
注意 |
---|
下面的代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型。 |
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <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>
C#
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <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>
.NET Framework 安全性
- AspNetHostingPermission 用于在宿主环境中进行操作。要求值:LinkDemand;权限值:Minimal。
- AspNetHostingPermission 用于在宿主环境中进行操作。要求值:InheritanceDemand;权限值:Minimal