Mike Borozdin's Blog

A blog about programming, web and IT in general

CSS Class Names Instead of ASP.NET Client IDs

In ASP.NET the client IDs of controls are often really unpredictable. When you need to add some JavaScript code that works with rendered controls you have to know their IDs. But usually an ID looks like something like this: “ctl00_contentBody_txtStreet”. There are several methods for overcoming that difficulty explained here. However they require writing additional code or even creating your own controls inherited from the original ones.

But there is one simply but yet dirty way of addressing HTML elements rendered by ASP.NET. Do you remember that you can assign the CssClass attribute to any server control? Do you remember the jQuery selector that allows you to retrieve the elements with the specified CSS class? So, we can use it!

The pattern is simple:

<asp:TextBox ID="txtStreet" runat="server" CssClass="txtStreet" />

You just add the CssClass attribute and give it a unique value.

Then you can retrieve the value of the element or perform any other manipulation with jQuery:

alert($('.txtStreet').val());

Pretty simple. Although you must remember that it can take some time in case you have a long page because it will look through all the HTML tags to find the one with the given class name. At the same time for fast execution you can specify which elements to look for, you can simply add a tag name before, for instance “input” or a jQuery attribute – “:input”:

alert($('input.txtStreet').val());

Tags:
Posted by Mike Borozdin on Thursday, February 26, 2009 1:32 AM GMT
  Shout it Kick it!  
Permalink | Comments (9) | Post RSSRSS comment feed

Comments

DotNetKicks.com

Thursday, February 26, 2009 1:36 AM GMT

trackback

Trackback from DotNetKicks.com

CSS Class Names Instead of ASP.NET Client IDs

DotNetShoutout

Thursday, February 26, 2009 1:36 AM GMT

trackback

Trackback from DotNetShoutout

CSS Class Names Instead of ASP.NET Client IDs

amrelgarhy Egypt

Thursday, February 26, 2009 2:55 AM GMT

amrelgarhy

and what about if we need to give the textbox a css class?

Kazi Manzur Rashid United States

Thursday, February 26, 2009 3:16 AM GMT

Kazi Manzur Rashid

I think you should also mention:

"This will apply to all the elements that has this class name. To apply on a single element you should assign unique class name".

Mike Borozdin Russia

Thursday, February 26, 2009 3:35 AM GMT

Mike Borozdin

amrelgarhy,

You can always assign style to multiple CSS classes at once, like:

.txtFirstName, txtLastName, txtEmail { width: 200px; }

Kazi Manzur Rashid

Exactly!

Artem Russia

Thursday, February 26, 2009 3:53 AM GMT

Artem

Delete my previous comment please Smile
I'm always using ScriptManager.RegisterStartupScript to get ClientIDs of controls.
msdn.microsoft.com/.../...gisterstartupscript.aspx

Sam United States

Tuesday, April 07, 2009 12:56 PM GMT

Sam

alert($('input.txtStreet:first').val()); would make it more specific-safe (if you don't want to accidentally edit other forgotten elements of the same class) and it should stop the script sooner.  

Mike Borozdin Russia

Tuesday, April 07, 2009 1:00 PM GMT

Mike Borozdin

Thanks, Sam Smile!

Web Development Community

Tuesday, May 05, 2009 9:17 AM GMT

trackback

Trackback from Web Development Community

CSS Class Names Instead of ASP.NET Client IDs

Comments are closed