Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday 28 February 2016

HOW TO PROGRAMMATICALLY SET THE FORECOLOR OF A LABEL IN ASP.NET C#


Today in Engineer's World, we are discussing ASP.NET & C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


How to programmatically set the ForeColor of a Label in Asp.net C#

In this, blog we are showing you how to set the color of the label control dynamically. For this, we have created a web form where two numbers are added and their result is printed onto the label control.

Practical Implementation: -

Design code (dynamicSetLabelControlForeColor.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicSetLabelControlForeColor.aspx.cs"
    Inherits="dynamicSetLabelControlForeColor" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            Online Add Two Number</h3>
        <table>
            <tr>
                <td>
                    Number A:&nbsp;
                </td>
                <td>
                    <asp:TextBox ID="txtNumA" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Number B:&nbsp;
                </td>
                <td>
                    <asp:TextBox ID="txtNumB" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnAdd" runat="server" Text="ADD" OnClick="btnAdd_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMsg" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

We can set the Fore Color of a label control dynamically in 2 ways. We have shown both the options. First, is the most common way to set the fore color of the label control. But, you can also try the second one.

Click imagination hunt to see latest Blogs

Practical Implementation:

Design code (dynamicSetLabelControlForeColor.aspx.cs)

using System;

public partial class dynamicSetLabelControlForeColor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        double sum = 0.00;
        double a = 0.00;
        double b = 0.00;

        a = Convert.ToDouble(txtNumA.Text.Trim());
        b = Convert.ToDouble(txtNumB.Text.Trim());
        sum = a + b;
        lblMsg.Text = "Sum of " + a + " + " + b + " = " + sum;

        /*Ist way*/
        lblMsg.ForeColor = System.Drawing.Color.Green;

        /*IInd way*/
        if (lblMsg.ForeColor != System.Drawing.Color.Green)
        {
            lblMsg.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblMsg.ForeColor = System.Drawing.Color.Empty;
        }
    }
}

OUTPUT:

Output for 1st Way



Output for 2nd Way



Drawbacks of 2nd Way:

Drawback Reason-1: For the first time, when the control reaches the line ->

if (lblMsg.ForeColor != System.Drawing.Color.Green)

The label color is empty. It sets the label color to green. But, when you press the ADD button for the second time. It skips, ‘if’ statement because now the label color is green so the control reaches the else statement and set its color to empty. Implies, method 2 sets the label color to green only for the very first time and for the rest turn it sets their color to empty/default i.e. black.

Drawback Reason-2: The number of lines of code used is more than method 1.


For any query, comment us below.


Related Questions: -


Q-1 What namespace contains the color class for asp.net?
A) using System;
B) using System.Color;
C) using System.Drawing;
D) using System.ForeColor;
Ans. Option(C). using System.Drawing;
Explanation: Read previous article on System.Drawing

Q-2 What prefix used for label control?
A) lbl
B) lab
C) No such prefix defined for Label control
Ans. Option(A).

Click imagination hunt to see latest blogs

Keep learning and sharing…

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...