DotNet Tutorials

Server Intellect

 Using CSS and Changing them Dynamically ASP.NET & VB

This tutorial shows how to implement CSS StyleSheet usage with ASP.NET and how to change them dynamically. VB 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:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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" />

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


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:

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub SwitchStylesheets(ByVal sender As Object, ByVal e As EventArgs)
If radioDark.Checked Then
stylesheet.href="dark.css"
End If
If radioLight.Checked Then
stylesheet.href="light.css"
End If
End Sub
End Class

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.




Looking for the C#.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!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect