Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 25521 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Dynamically Change CSS on Button Click in ASP.NET

Dynamically Change CSS on Button Click in ASP.NET

Article posted by Raj.Trivedi on 2/17/2013 | Views: 4344 | Category: ASP.NET | Level: Beginner | Points: 250 red flag

Advertisements

Advertisements
This article will explain on how css can be changed dynamically on button click in asp.net

Introduction


Hello friends, in this article we will see how to change the CSS of asp.net application dynamically.
In this article, button is being used to change the CSS using the Button Click event.


Objective


There are lots of time we want to change the background dynamically and also the look and feel of the website and you regret using Themes because it only works for asp.net controls and not to HTML Controls.Hence we can use CSS. For simplicity asp.net controls are used in this article,however HTML controls can be used as well.

Using the code


We have 1 label and 2 buttons, The first button will change the background of the webpage to maroon color and font family and the look of the label as well and the second button will change the background color to yellow and the font and the labels look and feel as per CSS.

To work around we have to implement a default look and feel,hence for that i have kept the background color to aqua.so when the page will load for the first time it will have the look and feel of the default css and on button clicks it will change the css and apply the changes as per css rule.

Now to change the CSS dynamically on button click we will use the command argument value given to the button in HTML mark up.

The command argument is the name of the css with extension of the css as well.

In all the css i have a common class .label so that i can also change the look and the feel of the label as well as per the changing the css.

For this i have assigned .label class to the label control.

Make a note that the default.css should have the .label class so that you can apply the changes to the label.



************Using the code and understanding the code*************************

<link id="lnkCSS" href="css/Default.css" runat="server" rel="stylesheet" type="text/css" /> 
The above line if you check i have set the runat = server mode to the link so that i can get this link on code behind to change its properties on code behind.

Block of code should be set style as "Code" like below.
// HTML Mark Up
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dymaic Change of Css.aspx.cs" Inherits="Dymaic_Change_of_Css" %>

<!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></title>
    <link id="lnkCSS" href="css/Default.css" runat="server" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblseeEffect" runat="server" Text="This is a Label" CssClass="label"></asp:Label><br /><br />
    <asp:Button ID="btnCss1" runat="server" CommandArgument="Brown.css" 
            Text="Apply Brown Theme" onclick="btnCss1_Click" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnCss2" runat="server" CommandArgument="Yellow.css" 
            Text="Apply Yellow Theme" onclick="btnCss2_Click" />
    </div>
    
    </form>
</body>
</html>

//Default.css
body 
{
	background-color:Aqua;
	font-family:@Adobe Gothic Std B;
	font-size:15pt;
}

.label
{
    font-weight:bold;
    color:Red;
}
//Brown.css
body 
{
	background-color:Maroon;
	font-family:@GulimChe;
	
}

.label
{
    font-weight:bold;
    color:Yellow;
    font-size:20pt;
}
Yellow.css
body 
{
	background-color:Yellow;
	font-family:Adobe Caslon Pro Bold;
}

.label
{
	color:Maroon;
	font-size:15pt;
	
}
//Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void btnCss1_Click(object sender, EventArgs e)
    {
        lnkCSS.Attributes["href"] = "~/css/" + (sender as Button).CommandArgument; // this will change the css as per the command agruement given in the HTML mark up
    }
    protected void btnCss2_Click(object sender, EventArgs e)
    {
        lnkCSS.Attributes["href"] = "~/css/" + (sender as Button).CommandArgument;// this will change the css as per the command agruement given in the HTML mark up
} }

Conclusion

I hope this would be beneficial for the developers.
Please keep Sharing...
"Sharing is Caring"


Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Raj Trivedi

Experience:2 year(s)
Home page:http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Member since:Saturday, June 16, 2012
Level:Starter
Status: [Member]
Biography:Raj.Trivedi i.e. me an
enthusiastic free lancer on .NET technology,
Tech Lead in development of www.getjobsindia.net , blogger and content writer on technology and on road to be a poet and Lecturer by Profession.
You can find my Key skills on
http://www.skillpages.com/Raj.Trivedi
I believe in "Sharing is Caring".
I help people on www.fixya.com with the computer problems and also write blogs on Technology,Humour and Culture
You can also follow me on my Blog i.e. http://kcwithfun.blogspot.in/ .I have also contributed in development of www.pcps.in,www.ticketplease.com and www.cinemax.co.in websites and Posting blogs on www.googlydoodly.com
 Responses
Posted by: Rimi1289 | Posted on: 19 Feb 2013 12:35:24 AM | Points: 25

Nice article of course.

Rimi.

Posted by: Raj.Trivedi | Posted on: 10 Mar 2013 12:33:41 PM | Points: 25

Thanks Rimi

>> Write Response - Respond to this post and get points
Related Posts

Hi all i am sure many of us has faced problem of dropdown comming above the div here is a small solution for this May be you can try this it really works.....

A tutorial video which explains step by step to enable your asp.net application to consume application services like Membership,Roles,Profiles etc.

.NET framework offers multiple options for achieving the development tasks and it is always challenging to decide when to use what. The data repeater controls in .NET i.e. Repeater, User Control, Custom control, dropdown list etc offers functionality of repeating the data on pages and we would discuss about those controls and their usage.

Web pages rarely be stand alone. Web applications almost always need to track users who visits multiple pages, whether to provide personalization, store information about a user or to track usage for reporting purposes.

In this Article you can learn how to get a Selected Row Data from a DataList and display the data in textBoxes.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 4:08:22 AM