HOW TO:讀取 Web Form 網頁中控制項的 HTML 屬性

更新:2007 年 11 月

這個範例會讀取 TextBox Web 伺服器控制項和 HtmlInputButton 控制項呈現的屬性。程式碼會讀取每個個別控制項之 AttributeCollection 物件的 Keys 集合。針對每個索引鍵項目,程式碼會取得相對應 Item 項目的值,以擷取索引鍵項目的相對應值。

範例

Response.Write(Button1.Attributes.Item("Style") & "<br />")
Dim key As String
For Each key In Button1.Attributes.Keys
    Response.Write(key & "=" & Button1.Attributes.Item(key) & "<br />")
Next
For Each key In Submit1.Attributes.Keys
    Response.Write(key & "=" & Submit1.Attributes.Item(key) & "<br />")
Next
Response.Write(Button1.Attributes[("Style")] + "<br />");

//String key;

foreach ( String key in Button1.Attributes.Keys)
{
    Response.Write(key + "=" + Button1.Attributes[key] + "<br />");
}

foreach ( String key in Submit1.Attributes.Keys)
{
    Response.Write(key + "=" + Submit1.Attributes[key] + "<br />");
}

編譯程式碼

這項範例需要:

  • ASP.NET Web 網頁。

  • 名為 Button1 的 Button Web 控制項。

  • ID 屬性設定為 Submit1 的 HtmlInputButton 控制項。

穩固程式設計

如果您指定的 Item 項目屬性名稱無效,則傳回值是空字串。

Web 伺服器控制項只會傳回不具有相對應強型別屬性 (Property) 的屬性 (Attribute)。

請參閱

工作

HOW TO:設定 ASP.NET Web 網頁中控制項的 HTML 屬性