Digital Colony!

ASP:Label Control and ViewState

One of the tips that is thrown around for improving the speed and performance of an ASP.NET page is to minimize the use of server controls. This makes sense for data-bound controls, but what about the asp:Label control? I decided to create a test to answer this question.

Why Use the asp:Label Control?

I've been coding HTML since 1995, but I have to confess that until I read ASP.NET 2.0 Unleashed by Stephen Walther, I had never used the HTML label control. Here is what I learned.
Always use a Label control with an AssociatedControlID property when labeling form fields. This is important when you need to make your website accessible to persons with disabilities. If someone is using an assistive device, such as a screen reader, to interact with your website, the AssociatedControlID property enables the assistive device to associate the correct label with the correct form field.

Using the asp:Label control will render the HTML label control in the following manner.
<asp:Label ID="lblFirstName" runat="server" Text="First Name" AssociatedControlID="txtFirstName" />
<label for="txtFirstName" id="lblFirstName">First Name</label>

The Test

I constructed a basic Form with the fields of FirstName, LastName, Address1, Address2, City, State and Zip Code. Page A used the asp:Label control which renders the label control. Page B avoided using the server control and used just the HTML label for each form field. Page C decided not to be concerned with persons with screen readers and not use any labels.

The Results

Page A (Server Label): 7 KB, ViewState = 52 chars
Page B (Client Label): 7 KB, ViewState = 52 chars
Page C (No Label)    : 6 KB, ViewState = 52 chars

Conclusion

Use the asp:Label control and the AssociatedControlID to label each form field. You get no performance penalty. The upside is your form will be accessible by more readers and assuming you don't make other mistakes, your page will be Section 508 compliant.

Labels: , , ,

AddThis Social Bookmark Button

0 Comments:

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.