 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 10th, 2010, 08:34 AM
|
Registered User
|
|
Join Date: Nov 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
How to use a control's ClientID in embedded style sheets
Hi,
in chapter 8 there's a note about how the asp.net runtime can change html control's 'id' attribute making it difficult to use css id selectors in some cases. The note goes on to say "Alternatively you can use the control's ClientID when using embedded style sheets", and I don't understand that sentence...how can the ClientID be used in style sheets and why's it limited to embedded style sheets?
|

December 10th, 2010, 12:06 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Imagine that your client ID ends up like this:
ctrl00_somecontrol_someid
Then in an external CSS file you could use:
#ctrl00_somecontrol_someid
{
// rules here
}
to target that element by its ID.
For embedded or in-line styles you don't need this as you could (for embedded) directly use the client ID:
#<%= yourControl.ClientID %>
{
// rules here
}
For in-line CSS this is not relevant at all since you define the CSS directly on the control's element.
Note that this should be a last resort as it's pretty fragile (rename or move the control and things break). It's much easier to use class selectors here than id selectors.
Cheers,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

December 10th, 2010, 12:51 PM
|
Registered User
|
|
Join Date: Nov 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
ah, thanks. I didn't realise that this sort of thing was possible:
#<%= yourControl.ClientID %>
and I can't find any examples of it being used.
At the risk of sounding ignorant, why doesn't the same work with external css?
|

December 10th, 2010, 01:35 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It doesn't work with external references to CSS files because yourControl.ClientID has no meaning in a CSS file. A request for a CSS file is a separate request, and from the perspective of the ASP.NET processing pipeline completely unrelated to the page that references it. So, the CSS file is a) not processed by .NET and b) even it were, it wouldn't understand the reference to your control.
Hope this helps,
Imar
|

December 10th, 2010, 05:01 PM
|
Registered User
|
|
Join Date: Nov 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
hmm, so embedded css and css in an external file are processed in completely different ways (the former is processed by .Net, the latter isn't)?
|

December 10th, 2010, 06:13 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
More or less....
External CSS files are treated as static files and served by IIS directly without involvement of .NET
Embedded CSS in an ASPX page isn't really processed by .NET either; it's plain content and streamed to the browser directly. However, <%= yourControl.ClientID %> *is* processed by .NET because of the server side tags. In this example, this code writes the ID of the control to the output stream where it just happens to end up in an embedded CSS code block, making it CSS. You could use this code in another location and it would end up as HTML or text.
Does this clarify things?
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

December 10th, 2010, 06:50 PM
|
Registered User
|
|
Join Date: Nov 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
ok, I finally got it = thanks Imar!!
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Style sheets & Menus |
arnabghosh |
Dreamweaver (all versions) |
1 |
October 27th, 2005 04:09 PM |
|
 |
|