D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

Overriding Unique ID's in Web Controls

Wednesday, February 28, 2007 9:08 AM

I came accross a scenario where I needed to override the ASP.NET-assigned id name of a label control that is used within a datalist control on a webform.

The problem is that, because of ASP.NET's naming scheme, you get names like

ctl00_ContentPlaceHolder1_NameOfList_ctl00_NameOfField

(Read the MSDN article that explains this here.)

Not the easiest or nicest to play with when you need to do something as simple as, say, use anchors on a page for navigation.

To make a long story short, there are two ID values for a web control:

The ID value (which is what you think it would be...a text value identifying the control)

The UniqueID value (which is assigned by ASP.NET to ensure that there are no duplicate names in the form)

See, I get why they did this, and it makes sense. But it just means that if you're in a situation where you need to have control over the UniqueID you're hooped...its a read-only property.

Rick Strahl to the rescue. He outlines in a blog post from last year the simplist way of overcoming this: create a class that inherits the Label control and override the UniqueID property so it outputs the regular ID value instead. (Read the post here.)

Tres-easy.

D




Feedback

No comments posted yet.


Post a comment