You must Sign In to post a response.

How to change label text color in C#

I have a asp page and there is a textbox, a label and a button.

When i input something into textbox and click the button label show the textbox value.

But if any wrong data in textbox then label text color will change to red(just text color will be changed not background or foreground color)

Plz help me to do so.

Thanks in advance.


Comments

Author: Reach2Shaik07 Sep 2011 Member Level: Gold   Points : 1

U need to use the ForeColor property of the label.

Author: sujatha07 Sep 2011 Member Level: Silver   Points : 3

 

To change label color for error msgs
lblMessage.ForeColor = System.Drawing.Color.Red;

For correct msgs use
lblMessage.ForeColor = System.Drawing.Color.Black;

Author: vasudeo rao vaka07 Sep 2011 Member Level: Silver   Points : 2







;































Change Background of Textbox

UserName:





Please enter UserName


Password:


ValidateEmptyText="true" ControlToValidate="txtPwd" ClientValidationFunction="changeColor">
Please enter Password


FirstName:



Please enter FirstName


LastName:


ValidateEmptyText="true" ControlToValidate="txtlname" ClientValidationFunction="changeColor">
Please enter LastName













Don't forget to rate this answer, if it is helpful...

Author: Jackuline07 Sep 2011 Member Level: Bronze   Points : 1

Label1.ForeColor = System.Drawing.Color.Green;

Author: Ultimaterengan08 Sep 2011 Member Level: Gold   Points : 2

Hi friend,
Just drag and drop one button,text box and label to your asp.net web page.
In button click event i checked one condition. if text box value is true label color will be changed or else not.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "Rengan")
{
Label1.ForeColor = System.Drawing.Color.Red;

}
else
{
Response.Write("Wrong Text");
}
}
}


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





Untitled Page












Author: SivaSaiKrishna08 Sep 2011 Member Level: Silver   Points : 0

yes ur code was right Rengan.

Thanks
SivaSaiKrishna

Author: DotNet15 Oct 2011 Member Level: Silver   Points : 1

Thanks very much to all of you.

Really all of yours suggestion were so helpful and my problem have been solved.
Hope that next time I will get more support from all of you.