Saturday, 22 March 2014

change label color in c#

Change the color of label in C#. In many cases; suppose you want to design your form or to highlight certain text;
I am just describing three ways to changing the fore color of label in c#.

@Change Label color in c# directly;
You can do the changes of label directly in HTL by adding the property of forecolor like:
  <asp:Label ID="lblDirectchange" ForeColor="Red" runat="server">Direct labal change</asp:Label>

@ Change Label color in c# CSS;
Change label color by style sheet;
Add style according to requiring;
<style type="text/css">
        .textChange
        {
            color: Green ;
        }
    </style>
Call this style method into your code
<asp:Label ID="lblbyCss"  CssClass="textChange" runat="server">Color by Css</asp:Label>
@Change Label color in c# dynamically
Changing the label text color by c# code which is written in server side;
Add code in HTML
<asp:Label ID="lblDynamicChange"  runat="server">Dynamically change color</asp:Label>
C# Code;
lblDynamicChange.ForeColor = System.Drawing.Color.Blue;
Full Code
Placing all the  HTML code together;
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .textChange
        {
            color: Green ;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblDirectchange" ForeColor="Red" runat="server">Direct labal change</asp:Label>
        <br />
        <br />
        <asp:Label ID="lblbyCss"  CssClass="textChange" runat="server">Color by Css</asp:Label>
         <br />
        <br />
        <asp:Label ID="lblDynamicChange"  runat="server">Dynamically change color</asp:Label>
    </div>
    </form>
</body>
</html>
Placing all the C# code together;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblDynamicChange.ForeColor = System.Drawing.Color.Blue;
    }
}
Output

No comments:

Post a Comment