SIGN UP MEMBER LOGIN:    
ARTICLE

Changing Style Sheet Dynamically

Posted by Srinivas Kotra Articles | ASP.NET Programming June 15, 2011
In this article I am posting code about applying style sheets dynamically in an ASP.Net web application.
Reader Level:


Cascading Style Sheets or CSS allow you to easily control the layout and look of your page. CSS tags or properties are easy to use and affect the look and feel or style of your pages. In this article I am posting code about applying style sheets dynamically in an ASP.Net web application.

--- .Css  Pages---

DayAndNight.css

Body {font-family:Arial, Verdana, sans-serif; font-size: 5px; color: #000000;}

{

/* your styles come here*/

}

 

Site.CSS

Body {font-family:Arial, Verdana, sans-serif; font-size: 5px; color: #000000;}

{

/* your styles come here*/

}


--- .aspx of master page---

Assigned runat ="server" property to access from server side.
 

<head>
<link  href="~/Styles/Site.css" rel="stylesheet" type="text/css"  runat="server" id="lnkStyleSheet"/>
</head>


In dropdown add styles sheet as a list item.

<asp:DropDownList ID="ddlStyles" runat="server" AutoPostBack="True" onselectedindexchanged="ddlStyles_SelectedIndexChanged">
<asp:ListItem Text="Original" Value="Styles/Site.css"></asp:ListItem>
<asp:ListItem Text="Day And Night" Value="Styles/DayAndNight.css"></asp:ListItem>
</asp:DropDownList>


--- .cs of master page ---

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if (Session["Theme"] != null)
                    lnkStyleSheet.Href = Session["Theme"].ToString().Trim();

                else
                    lnkStyleSheet.Href = ddlStyles.SelectedValue.Trim();

                if (Session["SelectedIndex"] != null)
                {
                    ddlStyles.SelectedIndex = Convert.ToInt32(Session["SelectedIndex"].ToString());
                }
              
            }
        }


 

        protected void ddlStyles_SelectedIndexChanged(object sender, EventArgs e)
        {
            lnkStyleSheet.Href = ddlStyles.SelectedValue.Trim(); // assigning style to link href in aspx page
            Session["Theme"] = ddlStyles.SelectedValue.Trim(); // storing selected value in session to maintain across the tabs
            Session["SelectedIndex"] = ddlStyles.SelectedIndex; // storing dropdown selected value in session  to maintain across the tabs
        }


Output

Original Style

Dymamic1.gif

Day and night Style

Dymamic2.gif

Please give me your comments and advice on this article.
Thanks,
Srinivas Kotra.

 

Login to add your contents and source code to this article
share this article :
post comment
 

worth reading this article..nice work..keep it up..

Posted by mohan kumar Feb 29, 2012

It is very good post. thnx :)

Posted by akhilesh Rao Jun 16, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor