Using ClientIdMode to name web server controls
ASP.NET is great at server-side processing, but how can you refer to ASP.NET web server controls at the client? The answer is to use the new(ish) ClientIdMode property to fix the controls' names.

Posted by Andy Brown on 10 October 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Use ClientIdMode to Refer to ASP.NET Controls  as HTML

The Problem

Consider the following web server control in ASP.NET:

Typical ASP.NET textbox control

A typical ASP.NET textbox control

By default, this is how this renders as HTML:

How textbox appears in HTML

How this textbox would render in HTML

The page on which the control sits uses a master page, and the cph stands for ContentPlaceHolder.

The question is: how could you refer to this textbox in client script, when you don't know how its name will be constructed? 

The Answer 

The answer is to use the web server control's ClientIdMode property:

The ClientIdMode property dropdown

The four possible values for this property

To see how this property works, the best two websites that I could find were these:

Website Notes
Dan Maharry's blog  Once you can get over the black background and difficult-to-read font, this gives an excellent summary of the possible property values above.
Rick Strahl's blog With a  more orthodox appearance,  this blog summarises the property values in slightly less detail.

Further Reading! 

Typical times when you might want to reference the client id property of a control are:

  • when you want to set the DefaultButton property of a form; 
  • when you want to apply the Focus method to a control to make it the active one;
  • when you want to use RegisterClientScriptBlock to attach client script to a form, which in turn should reference a particular control.

And since our youngest daughter has just come into the room I'm working in and started playing the piano, perhaps it's time to end there!

This blog has 0 threads Add post