Home > Code > C# > Alter CSS at runtime using C#

Alter CSS at runtime using C#

by Thamil   on Aug 11, 2014   Category: C#   |  Views: 980    |  Points: 25   |  Gold 


Below sample code shows how to alter the css at runtime, I have placed the label inside div and added runat attribute. In page load, I have assigned the css name to div.

.aspx page
-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm52.aspx.cs" Inherits="WebApplication1.WebForm52" %>

<!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>Untitled Page</title>
<link href="Style/Stylesheet1.css" rel="stylesheet" type="text/css" id="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<div id="divName" runat="server">
<asp:Label ID="Label1" runat="server" Text="Please enter the name"></asp:Label>
</div>
</div>
</form>
</body>
</html>

Stylesheet1.css
---------------
.lblStyle
{
text-decoration:none;
font-weight: bold;
font-size:12px;
color:#007ddd;
}

Code behind part
----------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
divName.Attributes["class"] = "lblStyle";
}
}





Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts