lable

表示在网页上显示文本的标签控件。

命名空间: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 属性自定义显示文本。

也可以使用 LiteralPlaceHolder 控件在 Web 窗体页上显示文本。但与 Label 控件不同的是,这些控件不会呈现任何附加标记。

Caution note警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

辅助功能

默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

下面的示例说明如何在网页上创建 Label 控件。

Note注意

下面的代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .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>
<%@ 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值