The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Response.AppendHeader, Webforms and favicons

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter van Ooijen

Posts: 284
Nickname: petergekko
Registered: Sep, 2003

Peter van Ooijen is a .NET devloper/architect for Gekko Software
Response.AppendHeader, Webforms and favicons Posted: Oct 31, 2005 9:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter van Ooijen.
Original Post: Response.AppendHeader, Webforms and favicons
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter van Ooijen
Latest Posts From Peter's Gekko

Advertisement

Perhaps I'm too naive on this but I'm stuck. Having turned the web upside down twice I'm still completely puzzled.

In the header of an aspx page's response is some meta-information to help the browser. One of the headers can be the stylesheet to use when rendering the page. Having linked a stylesheet in the webform designer (Format|Document styles in VS 2003) your page header will look something like this.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Onderwijs</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="StyleSheetIndato.css" type="text/css" rel="stylesheet">
</HEAD>
<body>

Linking a stylesheet this way has two disadvantages

  1. You have to repeat it for every page in the application
  2. It is hard to maintain, switching style sheets requires editing all aspx files

Being an OOP guy I consider the best way to link the sheet is in my page base class. The Response object has an AppendHeader method, so this could be a nice way to set the stylesheet:

 protected override void OnPreRender(EventArgs e)
 {
   base.OnPreRender (e);
   Response.AppendHeader("LINK", string.Format("rel='stylesheet' type='text/css' href='{0}'></link>", System.Configuration.ConfigurationSettings.AppSettings["StyleSheet"]));
 }
 

The code tries to add a header to the response which contains the link to the stylesheet.

Alas this does not work. In all my tries the AppendHeader method does nothing at all when the response is rendered by a webform. And I've found no nice event to hook into or method to override. I had this frustration before, at the time the solution was to wrestle in the stylesheet link in the page body pretending it was a snippet of script.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad (e);
    RegisterClientScriptBlock("MyStyleSheet", string.Format("<link rel='stylesheet' type='text/css' href='{0}'></link>", System.Configuration.ConfigurationSettings.AppSettings["StyleSheet"]));
}       
 

I'm still doing that, but recently revisited the subject and gave it another try. Another thing to put in a header is a shortcut icon, which is used in the browser's toolbar and favorites.

<HEAD>
  <LINK REL="SHORTCUT ICON" HREF="http://www.mydomain.com/myicon.ico">
  <TITLE>My Title</TITLE>
</HEAD>

This looks simple, but neither VS 2003 nor VS 2005 accepts even the markup. Let alone anything like Response.AppendHeader or injecting the link in the body will work.

As we say in Dutch it's giving me a pointed head. Any suggestion is more than welcome

Read: Response.AppendHeader, Webforms and favicons

Topic: VS 2005 and SQL 2005 RTMed Previous Topic   Next Topic Topic: Some inspiration for the young geeks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use