views:

362

answers:

3

The Problem

When using asp.net server controls id attributes such as the following are automatically generated.

<img id="ctl00_body_ULRepeater_ctl01_LIRepeater_ctl00_PartImg" src="img.png" />

While I'm not averse to id attributes in general, I try to stay away from using these unnecessarily verbose types of names and use concise, descriptive names.

The Question

Can I stop asp.net from generating these id attributes? They look terrible, and if I generate a lot of items with a repeater or something they actually add a good bit of page weight. How do I get rid of them?

Notes

I am using asp.net 3.0 in Visual Studio 2008.

[update]

Ok, so I can subclass (ClientID is declared overridable), but this is no fun really. I can use Literal Controls everywhere. Or I can grit my teeth and bear the painfully slow rendering of my pages with nearly nothing on them.

+3  A: 

I believe that one of the features coming in asp.net 4.0 will be the ability to better control the IDs that are generated. For now, you are going to get the name mangling for any server generated control. This is what allows asp.net to guarantee the uniqueness of your control's ID.

You can always use straight HTML markup (do not runat=server) to avoid this issue. You would be sacrificing the ease of use for a lighter weight page though.

Jim Petkus
Ugh. I was afraid of this. It seems that I cannot get around it in a repeater for the most part. Thanks for the answer Jim.
brad
A: 

As of now ControlID's are ReadOnly properties. In the upcoming release of ASP.NET Web Forms (with .NET 4.0) this will be a settable property using a number of different methods (such as static, inherit, etc.)

TheTXI
Really? Awesome! Do you have a link to any relevant articles about this?
teedyay
@teedyay: http://channel9.msdn.com/shows/10-4/10-4-Episode-3-ASPNET-WebForms-40/ includes a video demonstrating the different ways of naming Client ID's
TheTXI