ASP.NET Web Forms > Label and Literal

Applying CSS style from code behind in ASP.NET Web Forms

How to apply CSS style in the Label control from code-behind?


In case we need to change the CSS style of the text going to appear on the page, we can use this approach. 

ASPX Page
<p><asp:Label ID="Label3" runat="server" Text="My third Label"></asp:Label></p> 
CODE BEHIND
protected void Page_Load(object sender, EventArgs e) 
{
// writeing text from code behind
Label2.Text = "Written from code behind";
// Applying CSS style from code behind
Label3.Text = "Written from code behind";
Label3.Font.Bold = true;
string[] fontNames = {"Verdana", "Arial"};
Label3.Font.Names = fontNames;
Label3.Font.Size = FontUnit.Larger;
Label3.ForeColor = System.Drawing.Color.Green;
Label3.BackColor = System.Drawing.Color.Yellow;
}

CSS style can be applied 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 however you can use ForeColor and BackColor directly also.


 Views: 5321 | Post Order: 10



Write for us






Hosting Recommendations