WebControl.Attributes 属性

定义

获取与控件的特性不对应的任意特性(只用于呈现)的集合。

public:
 property System::Web::UI::AttributeCollection ^ Attributes { System::Web::UI::AttributeCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.AttributeCollection Attributes { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Attributes : System.Web.UI.AttributeCollection
Public ReadOnly Property Attributes As AttributeCollection

属性值

AttributeCollection

名称和值对的 AttributeCollection

属性

示例

以下示例演示Attributes了当控件失去焦点时TextBox如何使用 JavaScript 命令的属性WebControl来运行 JavaScript 命令。

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 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 xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>Attributes Property of a Web Control</title>
<script language="C#" runat="server">
 
        void Page_Load(Object sender, EventArgs e) {
           TextBox1.Attributes["onblur"]="javascript:alert('Hello! Focus lost from text box!!');";    
        }
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form id="form1" runat="server">
 
    <asp:TextBox id="TextBox1" columns="54" 
     Text="Click here and then tap out of this text box" 
     runat="server"/>  
 
 </form>
 </body>
 </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 xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>Attributes Property of a Web Control</title>
<script language="VB" runat="server">
 
        Sub Page_Load(sender As Object, e As EventArgs)
            TextBox1.Attributes("onblur") = "javascript:alert('Hello! Focus lost from text box!!');"
        End Sub
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form id="form1" runat="server">
 
    <asp:TextBox id="TextBox1" columns="54" 
     Text="Click here and then tap out of this text box" 
     runat="server"/>  
 
 </form>
 </body>
 </html>

注解

Attributes 集合包含 Web 服务器控件的开始标记中声明的所有属性的集合。 这样,你可以以编程方式控制与 Web 服务器控件关联的属性。 可以将属性添加到集合或从集合中删除属性。

备注

无论浏览器设置如何,此属性都呈现在控件的打开标记中集合中的所有属性。 并非所有浏览器都支持呈现的每个属性。 浏览器通常忽略不受支持的属性。

备注

不能使用Attributes集合将客户端脚本添加到WebControl实例。 若要添加客户端脚本,请使用 ClientScript 控件上的 Page 属性。

适用于

另请参阅