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

#630935    Author: Reach2Shaik    07 Sep 2011 Member Level: Gold   Points : 1

U need to use the ForeColor property of the label.


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

#630936    Author: sujatha  07 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;

#630942    Author: vasudeo rao vaka  07 Sep 2011 Member Level: Silver   Points : 2



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>

<script language="javascript" type="text/javascript">
function changeColor(source, args) {
var txtuser = document.getElementById('txtUsername');
var txtpwd = document.getElementById('txtPwd');
var txtfname = document.getElementById('txtfname');
var txtlname = document.getElementById('txtlname');
var strimg = new Array();
strimg = [txtuser, txtpwd, txtfname, txtlname];
if (args.Value == "") {
args.IsValid = false;
document.getElementById(source.id.replace('cv','txt')).style.background = 'orange';
}
else {
args.IsValid = true;
document.getElementById(source.id.replace('cv', 'txt')).style.background = 'white';
}
}
</script>;


</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2">
<b>Change Background of Textbox</b>
</td>
</tr>
<tr>
<td>
UserName:
</td>
<td>

<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvUsername" runat="server" SetFocusOnError="true" Display="Dynamic" ValidateEmptyText="true" ControlToValidate="txtUsername" ClientValidationFunction="changeColor">

<img src="Images/error.gif" align="middle"/>Please enter UserName
</asp:CustomValidator>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvPwd" runat="server" SetFocusOnError="true" Display="Dynamic"
ValidateEmptyText="true" ControlToValidate="txtPwd" ClientValidationFunction="changeColor">
<img src="Images/error.gif" align="middle"/>Please enter Password
</asp:CustomValidator>
</td>
</tr>
<tr>
<td>
FirstName:
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvfname" runat="server" SetFocusOnError="true" Display="Dynamic" ValidateEmptyText="true" ControlToValidate="txtfname" ClientValidationFunction="changeColor">
<img src="Images/error.gif" align="middle"/>Please enter FirstName
</asp:CustomValidator>
</td>
</tr>
<tr>
<td>
LastName:
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvlname" runat="server" SetFocusOnError="true" Display="Dynamic"
ValidateEmptyText="true" ControlToValidate="txtlname" ClientValidationFunction="changeColor">
<img src="Images/error.gif" align="middle"/>Please enter LastName
</asp:CustomValidator>
</td>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</td>
</tr>
</tr>
</table>
</div>
</form>
</body>
</html>




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

#630954    Author: Jackuline  07 Sep 2011 Member Level: Bronze   Points : 1

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

#631034    Author: Ultimaterengan  08 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" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Rengan"></asp:Label>
</div>
</form>
</body>
</html>




Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!

#631064    Author: SivaSaiKrishna  08 Sep 2011 Member Level: Silver   Points : 0

yes ur code was right Rengan.

Thanks
SivaSaiKrishna


Thanks nd Regards,

A.SivaSaiKrishna.

+91 9703885006.
E-Mail::sivasaikrishna11@gmail.com.

#637469    Author: DotNet  15 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.


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.