You can apply CSS style from code behind by accessing the properties of the control with the help of its Id. Most of the CSS styles can be applied using the Font property and its sub properties of the Label control. However you can use ForeColor and BackColor directly from the Label control.
TestApplyCSS.aspx
<%@ Page Language=”C#” AutoEventWireup=”false” CodeBehind=”TestApplyCSS.aspx.cs”
Inherits=”TestChange.TestApplyCSS” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”https://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Apply CSS style in the Label control from code-behind</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”LabelTest” runat=”server” Text=”My test label”></asp:Label>
</div>
</form>
</body>
</html>
TestApplyCSS.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class TestApplyCSS : System.Web.UI.Page
{protected void Page_Load(object sender, System.EventArgs e)
{string[] FontNames = {
“Arial”,
“Verdana”
};
LabelTest.Font.Names = FontNames;
LabelTest.Font.Bold = true;
LabelTest.Font.Size = FontUnit.Large;
LabelTest.ForeColor = System.Drawing.Color.Black;
LabelTest.BackColor = System.Drawing.Color.LightGray;}
}