How does ASP.NET Webforms decide HTML name of a control?

2.5k Views Asked by At

In ASP.NET web forms when a control is rendered on the page, the ID of each field is modified with the ctrl01 as needed to ensure collisions don't happen (aka myField becomes ctrl01$myField or whatnot).

I was wondering where one might find details on HOW the algorithm itself works, or where it might be I can find it. I am assuming the INamingContainer has something to do with this-- but alas I cannot find the part which actually decides the rendered field name.

Any help would be awesome!

4

There are 4 best solutions below

0
On BEST ANSWER

You are probably looking for this msdn article.

0
On

It's based on the hierarchical layout of the webpage. But you can control this with the ClientId property.

So a textbox in a usercontrol will be named ctrl01#textboxname (Like you said in your post)

0
On

Apart from the other answers, if you are using ASP .NET 4, you have much more control over it.

Take a look @ these web pages

http://www.west-wind.com/weblog/posts/54760.aspx

http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

0
On

It concatenates it's own name with your original id.

In ASP.NET 4 you can suppress this concatenation and keep your own id in three different ways:

  • Each server control has an attribute called clientIdMode which you can set to Static
  • You can also set clientIdMode in the page directive which will affect all controls on the page.
  • Or you can set it in the web.config to affect all controls in all pages. (Unless the page or control is set to a different clientIdMode

Note: If you are using AJAX Control Toolkit you will need to set those controls that are part of the toolkit to a clientIdMode of Predictive