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

定義

取得任意屬性 (Attribute) 的集合 (只供呈現),不與控制項上的屬性 (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 TextBox控制項失去焦點時, WebControl可以如何使用的屬性來執行 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 Forms 程式碼模型的詳細資訊,請參閱ASP.NET Web Form 頁面程式碼模型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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。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.

適用於

另請參閱