views:

202

answers:

3

I'm building an asp.net app using themes, and I've assigned a theme to the app using the web config.

I have a bookmark icon that I want to use for my page and it is located in the themes directory, but I am having trouble referencing the themes location from a link tag in my header.

First I tried putting a code block inside the link tags href element, which did not work. Instead all it did was html encode the <% characters and output it directly to the browser:

<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>

I am able to put a code block inside an element in an hr tag though, so I don't know why it won't work in a link tag:

<hr test="<%=Page.Theme %>"/>  

Then I tried doing a Response.Write inside the head tag, but I got an error saying the Controls collection cannot be modified because the control contains code blocks:

<% Response.Write("<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.Theme + "/images/bookmark.ico\" type=\"image/x-icon\"/>"); %>

I also tried it just with a string literal, and got the same error:

<%= "<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>" %>

Is there any way to reference something from the themes directory inside the link tag?

I'm trying to do this in both an ASP.NET 2 and an ASP.NET 2 MVC app.

A: 

Ok I got it working by using some inline code. Inline code is not my my first choice but I wanted a solution that worked in mvc as well. Here's what I came up with:

<head runat="server">
    <title>My Test App <title>
    <script language="CS" runat="server">
        void Page_Load(object sender, System.EventArgs e) 
        {
            string sTheme = String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme;
            litFacIcon.Text = "<link rel=\"shortcut icon\" href=\"/App_Themes/" + sTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>";
        } 
    </script>
    <asp:Literal ID="litFacIcon" runat="server"></asp:Literal>
</head>
Jeremy
+1  A: 

This wouldn't work because you marked it as runat="server"

try this instead

<link rel="shortcut icon" href="<%=ResolveUrl(string.Format("~/App_Themes/{0}/images/bookmark.ico", Page.Theme)) %>" type="image/x-icon"/>
hunter
Does not work either. Still get the error The Controls collection cannot be modified because the control contains code blocks (i.e. ).
Jeremy
can you tell me what your entire <head> tag looks like?
hunter
if you have runat="server" in your head tag and you add any other controls to it you couldn't do what I suggested
hunter
@hunter: Do you know why runat="server" makes it fail? I would think it would be the other way round
Casebash
tags marked with `runat="server"` can't be modified inline since ASP.Net is managing that control.
hunter
+1  A: 

jeremy,

try creating an html helper such as:

public static string SetThemeIcon(this HtmlHelper html, string themename)
{
    var filePath = VirtualPathUtility.ToAbsolute("~/App_Themes/" + themename + "/images/bookmark.ico");
    return "<link rel=\"shortcut icon\" href=\"" + filePath + "\" type=\"image/x-icon\"/>";
}

then, in your view or master page, simply reference it as so:

<%= Html.SetThemeIcon("test") %>

or as in your case above (in mvc):

<%= Html.SetThemeIcon(String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme) %>

hope this helps...

jim

jimi