How to apply CSS style in the Label control from code-behind in ASP.NET in VB.NET

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=”vb” AutoEventWireup=”false” CodeBehind=”TestApplyCSS.aspx.vb”

Inherits=”TextChange.TestApplyCSS” %>

<!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>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.vb

Public Class TestApplyCSS

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim FontNames As String() = {“Arial”, “Verdana”}

LabelTest.Font.Names = FontNames

LabelTest.Font.Bold = True

LabelTest.Font.Size = FontUnit.Large

LabelTest.ForeColor = Drawing.Color.Black

LabelTest.BackColor = Drawing.Color.LightGray

End Sub

End Class