Control.ClientID with CSS

Is it possible to use css with controls that generate a ClientID, rather than a regular ID?

Now I can use cssClass, but I want to know if id is possible:

#DivParrentId #LAbelControlId { padding:100px; color:Red; } 
+4
source share
2 answers

There is no good way to do this. Many developers use something like this:

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

But to be honest, I'm not a big fan of this. It is very bright and fragile. Starting with ASP.NET 4.0, you will be able to control the ClientID of the element in the code, so you can specify the identifiers for the elements that, as you know, you need to configure using CSS or JS.

In the meantime, I recommend using classes. In the system I'm working on, we need classes and ID bans for any client code (CSS or JS), because it's just not worth the extra overhead. Classes can work perfectly as identifiers - nothing prevents you from having many unique (one-time) classes, and there is no real problem with this method in terms of maintainability.

+8
source

This can be possible in several ways, none of which are elegant.

Perhaps you can predict the generated identifiers:

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

You can dynamically create css class names

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

You can override the ClientID of your shortcuts

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

Source: https://habr.com/ru/post/1286685/


All Articles