Implementing style sheet code dynamically

February 13, 2008 08:58 by billy

An interesting article about adding styles in .Net and JavaScript, although the code is in VB.Net you get the idea!

http://aspalliance.com/1546_Implementing_style_sheet_code_dynamically.1


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

February 13. 2008 13:17

I've been experimenting with this today for a site i'm doing

The authored site was raw html and used the default website, whareas min sits underneath the default website in it's own subdirectory. Needless to say, pathing for javascrip and CSS was an issue!!

You can add all of the header links via code-behind, and these can use the magic tildé (~) which always goes back to the application root.

Shazzam. i'll post more of my findings later :¬)

tim

February 13. 2008 17:21

Works like a charm :¬). I add a call to AddCSSLink() in my Page Load event, giving it the url of the CSS i'd like to include. .Net takes the tildétay and works out where in relation the requested page is compared to the CSS and adds the correct path

Shazam!!


protected void Page_Load(object sender, EventArgs e)
{
//Add the CSS link for default
AddCSSLink("~/media/css/default.css");
}

private void AddCSSLink(string LinkPath)
{
HtmlLink lnkCSS = new HtmlLink();
lnkCSS.Href = LinkPath;
lnkCSS.Attributes.Add("rel", "stylesheet");
lnkCSS.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(lnkCSS);
}

tim

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 31. 2010 20:17

Calendar

July 2010
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567