WebControl.Attributes WebControl.Attributes WebControl.Attributes WebControl.Attributes Property

定义

获取与控件的特性不对应的任意特性(只用于呈现)的集合。Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.

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

属性值

示例

下面的示例演示了Attributes WebControlTextBox控件失去焦点时,如何使用的属性来运行 JavaScript 命令。The following example illustrates how the Attributes property of a WebControl can be used to run a JavaScript command when the TextBox control loses focus.

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,则可能无法正常工作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。This code sample must be copied into an empty text file that has an .aspx extension. 有关 Web 窗体代码模型的详细信息,请参阅ASP.NET Web 窗体页代码模型For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.


<%@ 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 服务器控件的开始标记中声明的所有属性的集合。The Attributes collection contains a collection of all attributes declared in the opening tag of a Web server control. 这使您能够以编程方式控制与 Web 服务器控件关联的属性。This allows you to programmatically control the attributes associated with a Web server control. 可以向集合添加特性,也可以从集合中移除特性。You can add attributes to the collection or remove attributes from the collection.

备注

此属性在控件的开始标记中呈现集合中的所有属性,而不考虑浏览器设置。This property is rendered with all attributes in the collection in the control's opening tag, regardless of the browser settings. 并非所有浏览器都支持呈现的每个属性。Not all browsers support every attribute that is rendered. 浏览器通常会忽略不支持的属性。The unsupported attributes are usually ignored by the browser.

备注

不能WebControl Attributes使用集合向实例添加客户端脚本。You cannot add client-side script to a WebControl instance using the Attributes collection. 若要添加客户端脚本,请使用ClientScript Page控件上的属性。To add client-side script, use the ClientScript property on the Page control.

适用于

另请参阅