using css in a user control

A

Andy B

How do you use css in a user control? I need to put the css in a stylesheet
outside of the control.
 
B

Brian Lowe

Your user control's output is destined to be included in-line with HTML in
the rendered page, so it's OK to add attributes to your controls to refer to
CSS classes, id, etc. so long as the CSS style sheet loaded by the page has
corresponding classes, ids etc., all will be sweet.

You're not thinking of adding a stylesheet link <link
href="../css/General.css" rel="stylesheet" type="text/css" /> as part of
your user control content are you? That's not going to work (well, some
browsers may accept this kind of sloppy practice but flags should be raised
about breaking W3C standards compliance). If the rules you need are not in
the page's stylesheet you *could* add some code in the control's load event
to add an additional styelsheet link to the <head> of the containing page,
but I suspect that's more effort than you're aiming for.
 
A

Andy B

"Your user control's output is destined to be included in-line with HTML in
the rendered page, so it's OK to add attributes to your controls to refer to
CSS classes, id, etc. so long as the CSS style sheet loaded by the page has
corresponding classes, ids etc., all will be sweet."

That is more the less what I was thinking of.

"You're not thinking of adding a stylesheet link <link
href="../css/General.css" rel="stylesheet" type="text/css" /> as part of
your user control content are you?"
Nope. wouldn't work anyways. Tried it in an experiment when I first started
with asp.net and figured out it doesn't work.

"That's not going to work (well, some browsers may accept this kind of
sloppy practice but flags should be raised about breaking W3C standards
compliance). If the rules you need are not in the page's stylesheet you
*could* add some code in the control's load event to add an additional
styelsheet link to the <head> of the containing page, but I suspect that's
more effort than you're aiming for."

Maybe. I have a control that I will be making. It is a news control that
will get news from the database and show the articles on the page it is put
on. It will have ajax in it since I want to show the whole news article
without having to reload the whole page. It will have a property where you
can choose how many articles titles are displayed for that instance since I
plan on using it twice on the site. Once on the homepage and another time on
the news page. The homepage version shows 3 titles by default and the one on
the news page shows all titles. I was thinking of possibly having a content
block like the ones used in master pages for the stylesheets needed for each
particular page they are used on, but am wondering if the control load event
thing might be easier?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top