tags:

views:

44

answers:

3

I'm trying to pass the header object here:

<%=FileUtil.AddStylesheetToHeader(Header, "UsedItems.css") %>

In my Master page I have a <head runat="server">. And my aspx page definitely has a reference to my MasterPageFile in the page directive at the top of my MVC based .aspx.

I also have an import statement the namespace that the FileUtil class resides in :

<%@ Import Namespace="xxxx.Web.Utilities" %>

In standard ASP.NET you could reference the header with this.Header but in MVC I'm not able to do this...or I'm missing some kind of Imports or something.

for some reason though at runtime, with that call to AddStylesheetToHeader, I get the following error:

The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments.

I'm not sure why it's looking at a .NET type as I know when I mouseover my FileUtil at compile time it's definitely referencing xxxx.Web.Utilities.FileUtil.

In that method I'm using HtmlLink styleSheet = new HtmlLink(); I may not be able to use this as it's an ASP.NET Web control? Here's that method:

public static void AddStylesheetToHeader(HtmlHead header, string cssFilename)
{
    HtmlLink styleSheet = new HtmlLink();
    styleSheet.Href = "content/css/" + cssFilename;
    styleSheet.Attributes.Add("rel", "stylesheet");
    styleSheet.Attributes.Add("type", "text/css");

    header.Controls.Add(styleSheet);
}

I don't think I can use conrols that stem from System.Web.Controls since this is an ASP.NET application? If so, the how can I add a control to the header controls collection? Would I need to do this differently in MVC?

A: 

have you tried this.Request.Header?

No Refunds No Returns
System.Web.HttpRequest' does not contain a definition for 'Header'
CoffeeAddict
I think just passing Header in or this.Header is fine. I think I just don't have the right Import or doing something wrong...something fundamental that I can't see yet.
CoffeeAddict
+1  A: 

There may be a way to do it the way you're attempting, but it's more common in ASP.NET MVC to create a content placeholder in the <head> rather than accessing it programmatically. For example, your master view could look something like this:

<html>
  <head>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
  </head>
</html>

And your view could look like this:

<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
  <link href="/content/css/UsedItems.css" rel="Stylesheet" type="text/css" />
</asp:Content>
Jacob
ah, gotcha. That makes sense. Man it sure would be nice though to do it the way I was using that method like I did in standard ASP.NET
CoffeeAddict
A: 

You can use JavaScript to dynamically add content to your HEAD section as shown in the code below:

<script language="javascript" type="text/javascript">

     $(document).ready(function() {


         $("head").append("<link href='Content/Site.css' rel='stylesheet' type='text/css' />");

      });   

 </script>
azamsharp
doesn't seem to work. I put that in one of my content placeholders in my .aspx page.
CoffeeAddict
When you will view source you will NOT see the link added to the head. But if you use FireBug to see the DOM you will notice that the link is added to the head. You can then use the Site.css.
azamsharp