views:

77

answers:

2

I need to dynamically alter my stylesheet link in my masterpage.

I have the code below but the css isn't displaing correctly and the outputed HTML looks like below..

Outputed HTML

<link href="../Content/%3C%25=c.Area_Name%20%25%3E.css" rel="stylesheet" type="text/css" />

Stylesheet reference

<% foreach (var c in (IEnumerable<Categories>)ViewData["Categories"]) { %>
<link href="../../Content/<%=c.Area_Name %>.css" rel="stylesheet" type="text/css" />
<% } %>

Does anyone have any ideas?

A: 

The problem is that you can't put <%= %> code inside the head tag. Try the same code you did outside the heads tag.

VinTem
The problem is that I need to modify the Stylesheet reference in the head tag. What LukLed says above did work, not sure if this is the best way around the problem though?
Jemes
A: 

In markup place link with run at

<link runat="server" id="lnkCss1" rel="stylesheet"  type="text/css" media="print, projection, screen"/>

In code use

((HtmlLink)FindControl("lnkCss1")).Attributes["href"] = ResolveUrl("~/Css/YourCss.css");

Another solution is to dynamically add HtmlLink link without using link tag in markup.

Priyan R
Thanks Priyan, when you say 'In code use' where exactly would that code need to go?
Jemes
In page load of page/master page
Priyan R
I don't think I have a page behind in asp.net mvc.
Jemes