Manually changing the ID attribute of a control

by timvasil 11/2/2007 1:55:00 PM

The ID of a control, such as a <div runat='server' id='root'>, can be specified directly in the .aspx file.  But what if the ID needs to change dynamically?  For example, wth the YUI tookit you use the ID attribute of the root div to specify the page template, and you may want the template to change based on a user's preference.

In the code behind file, you may try changing the ID by writing something like root.ID = "doc3".  This may work, but if your control is in a naming container (such as a MasterPage) the control will render more like <div id='ctrl00$doc3'>.  There is a way to take complete control of the attribute, however:

   root.ID = null;
   root.Attributes[
"id"] = "doc3";

If you don't set the ID property to null then two ID attributes are rendered:  not what you want, and not valid HTML.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET

Related posts

Comments are closed

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments