Adding Stylesheets Programmatically in ASP.NET

Adding StyleSheets Programmatically in Asp.Net

Okay, here is the solution I am currently using :

I created a helper class :

using System.Web.UI;
using System.Web.UI.WebControls;

namespace BusinessLogic.Helper
{
public class CssAdder
{
public static void AddCss(string path, Page page)
{
Literal cssFile = new Literal() { Text = @"<link href=""" + page.ResolveUrl(path) + @""" type=""text/css"" rel=""stylesheet"" />" };
page.Header.Controls.Add(cssFile);
}
}
}

and then through this helper class, all I have to do is :

CssAdder.AddCss("~/Resources/Styles/MainMaster/MainDesign.css", this.Page);
CssAdder.AddCss("~/Resources/Styles/MainMaster/MainLayout.css", this.Page);
CssAdder.AddCss("~/Resources/Styles/Controls/RightMainMenu.css", this.Page);
//...

So I can add as much as I want with one line of simple code.

It also works with Masterpage and content page relationships.

Hope it helps.

P.S: I don't know the performance difference between this and other solutions but it looks more elegant and easy to consume.

Programmatically added stylesheet ASP.NET (VB)

I was able to figure out the solution to my problem by using Inspect element to find where the stylesheets were being loaded in. When you programmatically add a stylesheet to a content page, the stylesheet link gets added to the master web page head tag, not the content page head tag. My stylesheet was ultimately in the wrong directory (the stylesheet directory of the content page, NOT of the master page), and moving it to the master page's directory solved the issue.

programmatically apply style to an asp.net website

The <style> tag can also be used as server control:

<style type="text/css" runat="server" id="htmlCss"></style>

This will generate a field of type HtmlGenericControl in the page.

In one of the page life-cycle events (Page_Load, Page_Init, etc), assign the literal CSS definition like this:

var css = @"
body
{
background-color:#b0c4de;
}";
htmlCss.InnerHtml = css;

How can I switch out a stylesheet programmatically?

Give your stylesheet link element an id, then you should be able to change the href on it to something else. This code works in chrome, not sure about other browsers:

<link id="userStylesheet" rel="stylesheet" type="text/css" href="/css/green.css" />

Javascript to change it:

var linkTag = document.getElementById('userStylesheet');
linkTag.href = "css/maroon.css";

For the server side problem you could problem just redirect after they postback a change to it to get it to take effect on the next page load.

How to set stylesheet programmatically in an ASP.NET MVC 2 project?

Ok, Try removing the runat="server" tag from the <head> tag

Load css dynamically in asp.net?

A good approach for a WebForms-only project is to link to an .ashx handler in your page instead of a static CSS file:

<link rel="stylesheet" type="text/css" href="DynamicStyles.ashx" />

Then create the handler (add a 'Generic Handler' item from Visual Studio) and within it you can load the CSS from a database or wherever. Just make sure you set the content type correctly in the handler, so that browsers recognise the response as a valid stylesheet:

context.Response.ContentType = "text/css";


Related Topics



Leave a reply



Submit