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.

 

Tags:

ASP.NET

Comments are closed

Search

Calendar

«  February 2012  »
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

View posts in large calendar

Recent comments

Archive