user control stylesheets

  • Thread starter Thread starter Jill Graham
  • Start date Start date
J

Jill Graham

Hi,

I have an asp .net page which includes several custom user controls.
Each user control has its own template (.ascx file)

Now, I want my graphical people to be able to add a link to a stylesheet
file on top of the template file.
This link should then be included in the head section of the page.

This means that the user control should pass the stylesheet link to the page
and the page should insert the stylesheet link into its head section.

It looks like this :

my asp .net page :
-------------------
<html>
<head>
<ab:header id="_header" runat="server">
</head>
<body>
<ab:myControl id="_myControl" runat="server">
</body>
</html>


the .ascx file of myControl :
----------------------------
<ab:addCSS runat="server"><link rel="stylesheet" type="text/css"
href="/stylesheets/main.css"></ab:addCSS>
<table>
<tr><td>blablabla</td></tr>
</table>

The control addCSS in the template file should take the stylesheet link,
pass it to the parent page which includes the link in the ab:header
placeholder.


Jill
 
Jill, your user control can use the Page.RegisterClientScriptBlock
method to register the stylesheet link with the page it is hosted on.
A good place for this is in the User Control's OnInit() method, like
so:

protected override void OnInit(EventArgs e)
{
if( !this.Page.IsClientScriptBlockRegistered( "myStyleLink" ) )
this.Page.RegisterClientScriptBlock( "myStyleLink",
"<link rel=stylesheet href=css/someStyleSheet.css type=text/css
/>" );

base.OnInit (e);
}

Then, just expose a property for the user of your control to enter the
stylesheet path & filename. You can use that property and inject it
into the filename above.

Hope this helps,
Mike
 
Back
Top