How to change color of label in asp.net (Dynamic|static)

{0 Comments}

You can change asp.net label color in two ways, one is dynamically (code behind) other statically (css of source). Basically asp.net label get changed to span tag in run tag. So have a look on both of the ways to change color of labels in asp.net.

Static method:

To change color in static method you have to assign a css style property of label, it may be in line style or external css sheet. Following is the syntax for inline css which will change the color of label.

 HTML |  copy code |? 
1
<asp:Label ID="Label2" style="color:red" runat="server" Text="Label"></asp:Label>

Dynamic method:

To change label’s color dynamically in asp.net you can use following c# syntax which will help you to set up the css property of label dynamically below that syntax there is image which will give you a idea that how this works.

 HTML |  copy code |? 
1
<asp:Label ID="Label1" Width="313px" Style="word-break: break-all;" runat="server"
2
            Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been ...."></asp:Label>
3
        <asp:Button ID="btn_true" runat="server" Text="true" onclick="btn_true_Click" />
4
        <asp:Button ID="btn_false" runat="server" Text="false" 
5
            onclick="btn_false_Click" />
6
        

code behind:

 C# |  copy code |? 
1
protected void btn_true_Click(object sender, EventArgs e)
2
    {
3
        Label1.ForeColor = System.Drawing.Color.Red;
4
    }
5
    protected void btn_false_Click(object sender, EventArgs e)
6
    {
7
        Label1.ForeColor = System.Drawing.Color.Blue;
8
    }

result:

Leave a Comment

Your email address will not be published.