Search...

Thursday, March 1, 2012

Change StyleSheet at runtime in Asp.Net





************************************************************************

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change StyleSheet at runtime in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
 
    <div id="main">
    <div id="header">Header Text</div>
    <div id="content">Content</div>
    <div id="footer">Footer Text</div>
    </div>
 
 

    <div style="margin-top:20px;text-align:center">
        <asp:Button ID="Button1" runat="server" Text="Stylesheet 1" onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Stylesheet 2" onclick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" Text="Stylesheet 3" onclick="Button3_Click" />
    </div>
    </form>
</body>
</html>

************************************************************************

using System;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    private HtmlLink cssLink = new HtmlLink();
    protected void Page_Load(object sender, EventArgs e)
    {

       
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        cssLink = new HtmlLink();
        cssLink.Href = "StyleSheet1.css";
        cssLink.Attributes.Add("rel", "stylesheet");
        cssLink.Attributes.Add("type", "text/css");
        Header.Controls.Add(cssLink);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        cssLink = new HtmlLink();
        cssLink.Href = "StyleSheet2.css";
        cssLink.Attributes.Add("rel", "stylesheet");
        cssLink.Attributes.Add("type", "text/css");
        Header.Controls.Add(cssLink);

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        cssLink = new HtmlLink();
        cssLink.Href = "StyleSheet3.css";
        cssLink.Attributes.Add("rel", "stylesheet");
        cssLink.Attributes.Add("type", "text/css");
        Header.Controls.Add(cssLink);
    }
}

************************************************************************

/*StyleSheet1.css*/
#main
{
width:700px;
margin:0px auto;
border:1px solid #f7f5f1;
}

#header
{
background:#ffb420;
height:50px;
font-family:Arial,Verdana;
font-size:25px;
padding-top:20px;
}

#content
{
height:300px;
}

#footer
{
background:#ffb420;
height:20px;
font-family:Arial,Verdana;
font-size:15px;
padding:5px;
}

************************************************************************

/*StyleSheet2.css*/
#main
{
width:700px;
margin:0px auto;
border:1px solid #f7f5f1;
}

#header
{
background:#cfe7ff;
height:50px;
font-family:Arial,Verdana;
font-size:25px;
padding-top:20px;
}

#content
{
height:300px;
}

#footer
{
background:#cfe7ff;
height:20px;
font-family:Arial,Verdana;
font-size:15px;
padding:5px;
}

************************************************************************

/*StyleSheet3.css*/
#main
{
width:700px;
margin:0px auto;
border:1px solid #f7f5f1;
}

#header
{
background:#5fbe61;
height:50px;
font-family:Arial,Verdana;
font-size:25px;
padding-top:20px;
color:#ffffff;
}

#content
{
height:300px;
}

#footer
{
background:#5fbe61;
height:20px;
font-family:Arial,Verdana;
font-size:15px;
color:#ffffff;
padding:5px;
}

No comments: