Forums » .NET » ASP.NET »

How to change label text color in C#


Posted Date: 07 Sep 2011      Posted By:: DotNet     Member Level: Silver    Member Rank: 544     Points: 3   Responses: 7



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.




Responses

#630935    Author: Reach2Shaik      Member Level: Gold      Member Rank: 1268     Date: 07/Sep/2011   Rating: 2 out of 52 out of 5     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      Member Level: Silver      Member Rank: 601     Date: 07/Sep/2011   Rating: 3 out of 53 out of 53 out of 5     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      Member Level: Silver      Member Rank: 843     Date: 07/Sep/2011   Rating: 2 out of 52 out of 5     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      Member Level: Bronze      Member Rank: 5422     Date: 07/Sep/2011   Rating: 2 out of 52 out of 5     Points: 1

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

 
#631034    Author: Ultimaterengan      Member Level: Gold      Member Rank: 11     Date: 08/Sep/2011   Rating: 2 out of 52 out of 5     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      Member Level: Silver      Member Rank: 1487     Date: 08/Sep/2011   Rating: 2 out of 52 out of 5     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      Member Level: Silver      Member Rank: 544     Date: 15/Oct/2011   Rating: 2 out of 52 out of 5     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.


 
Post Reply

 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.



Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John

Online Members

teja rao
More...
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India