PositiveGuy
PositiveGuy

Reputation: 47743

How to get at Header

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?

Upvotes: 0

Views: 254

Answers (3)

azamsharp
azamsharp

Reputation: 20086

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>

Upvotes: 0

Jacob
Jacob

Reputation: 78860

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>

Upvotes: 1

No Refunds No Returns
No Refunds No Returns

Reputation: 8336

have you tried this.Request.Header?

Upvotes: 0

Related Questions