tags:

views:

243

answers:

2

Is it possible to use css with controls that generate ClientID and not regular ID?

I now I can use cssClass, but i want to know if it's possilbe to use id's:

#DivParrentId #LAbelControlId
{

    padding:100px;
    color:Red;
}
+3  A: 

There's not a good way to do this. Many developers use something like this:

<style>
#<% MyControl.ClientID %>
{
    padding:100px;
}
</style>

But frankly I am not a big fan of it. It's very tag-soupy and fragile. Starting with ASP.NET 4.0, you will be able to control the ClientID of an element in code, so you can specify IDs for elements you know you will need to target with CSS or JS.

In the meantime, I recommend using classes. In the system I work on, we require classes and ban IDs for any client-side code (CSS or JS) because it's just not worth the extra overhead. Classes can work just fine as IDs - nothing stopping you from having many unique (one-use) classes, and there's no real issue with working this way from a maintainability standpoint.

Rex M
A: 

It could be possible in several ways, none of which are elegant.

You may be able to predict generated id's:

#ctl$00_mycontainer_idofmylabel
{
    padding: 100px;
}

You can dynamically create your css class names

cssLiteral.Text = "#" + myLabel.ClientID + @"{ padding: 100px; }";

You can override ClientID of your labels

myCustomLabel.ClientID = "myHandCraftedId" // where myCustomLabel is an instance of some CustomLabel class that extends Label and overrides ClientID property.
Serhat Özgel