Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Read HTML Attributes for Controls in Web Forms Pages

This example reads the attributes rendered for a TextBox Web server control and an HtmlInputButton control. The code reads the Keys collection of the AttributeCollection object of each individual control. For each key item, the code extracts the corresponding value by getting the value of the corresponding Item element.

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 />");

This example requires:

  • An ASP.NET Web page.

  • A Button Web control named Button1.

  • An HtmlInputButton control whose ID attribute is set to Submit1.

If you specify an invalid attribute name for the Item element, the return value is an empty string.

Web server controls return only the attributes that do not have corresponding strongly typed properties.

Community Additions

© 2015 Microsoft