DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Using CSS and Changing them Dynamically ASP.NET & C#

This tutorial shows how to implement CSS StyleSheet usage with ASP.NET and how to change them dynamically. C# version. This tutorial will show how easy it is to implement CSS stylesheets to an ASPX page, and also how to let the user dynamically change the look and feel of the website in an instant. In the example below, we use radio buttons to change between a light and a dark look to the website. This is done with two separate CSS files:
Two simple example CSS files are as follows.
Dark.css:

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

body
{
color: #cc0033;
background-color: #996666;
}
.reverse
{
background-color: White;
color: Maroon;
}

Light.css:

body
{
background-color: White;
color: Black;
}
.reverse
{
background-color:Black;
color:White;
}

To attach the StyleSheet, we add the following code inside the <head> tags:

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

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


The ASPX code will look something like this.
Notice the radio buttons' OnCheckedChanged attributes.

<form id="form1" runat="server">
<div>
<asp:Label ID="CaptionLabel" runat="server" Style="font-size: 1.2em; font-family: Arial, 'Times New Roman', Verdana; background-color: #99ffff" Text="Label"></asp:Label>
<asp:TextBox ID="NumberTextbox" runat="server" Style="background-color:Blue; color:Yellow;"></asp:TextBox>&nbsp;
<asp:Label ID="ResultLabel" runat="server" Text="Label" CssClass="reverse"></asp:Label>
<asp:RadioButton ID="radioLight" runat="server" AutoPostBack="True" GroupName="grpSelectStylesheet" OnCheckedChanged="SwitchStylesheets" Text="Light" />
<asp:RadioButton ID="radioDark" runat="server" AutoPostBack="True" Checked="True" GroupName="grpSelectStylesheet" OnCheckedChanged="SwitchStylesheets" Text="Dark" />
</div>
</form>

The code simply switches the StyleSheets upon radiobutton click. The code-behind should look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void SwitchStylesheets(object sender, EventArgs e)
{
if (radioDark.Checked)
stylesheet.href="dark.css";
if (radioLight.Checked)
stylesheet.href="light.css";
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.




Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!