Using a Stylesheet in a User Control

N

Nathan Sokalski

I have a user control that uses classes from a CSS Stylesheet. Because a
User Control does not have a <head> section, I do not know where to put a
link tag to link it to the stylesheet. If I manually place a a link tag in
the page that I use the User Control in it works fine, but is there a way to
have the control add this link tag for me when I insert the control? Thanks.
 
S

Smithers

You can inject the link tag into the page that hosts the user control. You
could place a Literal control on the page, then set its .Text property to a
string containing the the link tag. You could do this at the same place in
your code where you insert the control.

-HTH
 
E

EJD

Hi,

The user control that I have in one of my projects looks like this in
the HTML:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="Header.ascx.cs" Inherits="MyProject.Header1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<LINK href="MyProject.css" type="text/css" rel="stylesheet">

Although I do use the same css file for all of the pages in the
project, if I just add the UC to a blank page without adding it to the
webform as well, it seems to work fine... HTH.
EJD
 
Joined
Sep 10, 2008
Messages
1
Reaction score
0
Add Style Sheet Reference to User Control

I added this to the Page_PreRender event in the User Control:

Dim Style As New HtmlControls.HtmlLink

With Style.Attributes
.Add("href", Me.ResolveUrl(Me.AppRelativeVirtualPath).Replace(".ascx", ".css"))
.Add("type", "text/css")
.Add("rel", "stylesheet")
End With

Page.Header.Controls.Add(Style)

This simply matches the name and location of the user control in terms of the name and location of the stylesheet. You could enhance this, of course, to use a different name or path for the stylesheet while still rendering the link to the stylesheet only on pages hosting your control.

- Jeff -
 
Joined
May 28, 2008
Messages
11
Reaction score
0
css class in web user control

Code:
we all know that inwebuser control there is no html,head tags in it.so...
To use stylesheet in a user control 
mention the cssclassname in web user control where needed in webuser control you donot find the changes according to css class.you can see the changes in  webform where you are using that webusercontrol.
now in webform in which you are using the webuser control add stylesheet in head section of the webform
you find the changes according to the stylesheet.
 
Joined
May 28, 2008
Messages
11
Reaction score
0
use cssclass inwebuser control

swati254 said:
we all know that inwebuser control there is no html,head tags in it.so...
To use stylesheet in a user control
mention the cssclassname in web user control where needed in webuser control you donot find the changes according to css class.you can see the changes in webform where you are using that webusercontrol.
now in webform in which you are using the webuser control add stylesheet in head section of the webform
you find the changes according to the stylesheet.
 

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
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top