Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add stylesheet using code-behind

Status
Not open for further replies.

tondarb

Programmer
Oct 8, 2003
37
US
Hi,
I am having problem in writing the stylesheet using C# code-behind. I have written a code, but it is not affecting my pages. Can any one please tell me how to write... This is the code

<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<link rel="stylesheet" type="text/css" media="all" href="../Css/landing.css" />

<style type="text/css">
/*<![CDATA[*/
#banner { background-image:url../images/banner1.jpg) }
#splash{background-image:url(../images/active_bg.jpg) }
/*]]>*/
</style>


and I have written like this....

protected void Page_Init(object sender, EventArgs e)
{
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Href = "~../Css/landing.css";
Page.Controls.Add(link);

HtmlLink linkMain = new HtmlLink();
linkMain.Attributes.Add("type", "text/css");
linkMain.Attributes.Add("rel", "stylesheet");
linkMain.Href = "#banner{ background-image:url(../images/banner1.jpg) }";
Page.Controls.Add(linkMain);

HtmlLink linkSplashButtons = new HtmlLink();
linkSplashButtons.Attributes.Add("type", "text/css");
linkSplashButtons.Attributes.Add("rel", "stylesheet");
linkSplashButtons.Href = "#splash{ background-image:url(../images/active_bg.jpg) }";
Page.Controls.Add(linkSplashButtons);
}

Can any please tell me how to write this



 
Maybe instead of writing it out as a link you should write it out as a literal. Also, which version are you using? If you're using VS2005, you can do page.header.add, or use a content place holder in the header. I think that you need to tell the page where to add it <header>.
 
Now this part is working
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Href = "../Css/landing.css";
Page.Controls.Add(link);

the error is..by mistake I have given like this
link.Href = "~../Css/landing.css";

Now for this two I am unable to find out what is the problem.

HtmlLink linkMain = new HtmlLink();
linkMain.Attributes.Add("type", "text/css");
linkMain.Attributes.Add("rel", "stylesheet");
linkMain.Href = "#banner{ background-image:url(../images/banner1.jpg) }";
Page.Controls.Add(linkMain);

HtmlLink linkSplashButtons = new HtmlLink();
linkSplashButtons.Attributes.Add("type", "text/css");
linkSplashButtons.Attributes.Add("rel", "stylesheet");
linkSplashButtons.Href = "#splash{ background-image:url(../images/active_bg.jpg) }";
Page.Controls.Add(linkSplashButtons);


Yes I am using VS2005. Actually I am working on content management system. It is having a master page,
and all the other aspx pages are having the asp:Content tags. Here I can't use the <head> tags. All the aspx pages are having different stylesheets. So for every aspx page I must add the stylesheets in code-behind.

While trying to add the stylesheets from code-behind I am facing problem.

Please help me


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top