Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Dynamically Setting the Forecolor Property in a Web Lable Control


Message #1 by "John Criswell" <John.Criswell@j...> on Mon, 21 Oct 2002 18:59:29
I have been working through the exercises in Ch 11, "Beginning ASP.NET 1.0 
with C#" and I'm having difficulty with ex 3: "Bind a drop-down list to an 
array containing five colors, then create a submit button that displays a 
line of text in the selected color when clicked."

I am trying to set the forecolor property of an asp label control and it 
turns out that it's read only.

Here is my code:

private void myDropDownList_SelectedIndexChanged(object sender, 
System.EventArgs e)
		{
			string strColor;
			strColor = myDropDownList.SelectedItem.Text;
			myLabel.Text = strColor;

			switch(strColor)
			{
				case "Red":
					myLabel.ForeColor = "Red";
					break;
				case "Blue":
					myLabel.ForeColor = "Blue";
					break;
				case "Green":
					myLabel.ForeColor = "Green";
					break;
				case "Yellow":
					myLabel.ForeColor = "Yellow";
					break;
				case "Brown":
					myLabel.ForeColor = "Brown";
					break;
				default:            
					myLabel.ForeColor 
= "Black";            
					break;
			}
		}

I'm trying to use the "SelectedIndexChanged" event of the drop down list 
to change the color of the ForeColor property in the label.  The label 
gets populated with the name of the color.  The text should change to the 
color of the text, i.e., the string "red" should change to the color red.

This code is not working because the ForeColor property appears to be read-
only.  Is there another way to accomplish this that works?

Thanks,

John Criswell
Message #2 by philp@w... on Tue, 22 Oct 2002 14:33:26
> I have been working through the exercises in Ch 11, "Beginning ASP.NET 
1.0 
w> ith C#" and I'm having difficulty with ex 3: "Bind a drop-down list to 
an 
a> rray containing five colors, then create a submit button that displays 
a 
l> ine of text in the selected color when clicked."

> I am trying to set the forecolor property of an asp label control and it 
t> urns out that it's read only.

> Here is my code:

> private void myDropDownList_SelectedIndexChanged(object sender, 
S> ystem.EventArgs e)
	> 	{
	> 		string strColor;
	> 		strColor = myDropDownList.SelectedItem.Text;
	> 		myLabel.Text = strColor;

> 			switch(strColor)
	> 		{
	> 			case "Red":
	> 				myLabel.ForeColor = "Red";
	> 				break;
	> 			case "Blue":
	> 				myLabel.ForeColor = "Blue";
	> 				break;
	> 			case "Green":
	> 				myLabel.ForeColor = "Green";
	> 				break;
	> 			case "Yellow":
	> 				myLabel.ForeColor = "Yellow";
	> 				break;
	> 			case "Brown":
	> 				myLabel.ForeColor = "Brown";
	> 				break;
	> 			default:            
	> 				myLabel.ForeColor 
=>  "Black";            
	> 				break;
	> 		}
	> 	}

> I'm trying to use the "SelectedIndexChanged" event of the drop down list 
t> o change the color of the ForeColor property in the label.  The label 
g> ets populated with the name of the color.  The text should change to 
the 
c> olor of the text, i.e., the string "red" should change to the color red.

> This code is not working because the ForeColor property appears to be 
read-
o> nly.  Is there another way to accomplish this that works?

> Thanks,

> John Criswell


Please note the information regarding book specific support questions in 
the P2P FAQ.  I would like to explain the Wrox support process. If you 
wish to directly query a problem in the book with an expert who knows the 
book in detail then e-mail support@w... , with the title of the book 
and the last four numbers of the ISBN in the subject field of the e-mail.  
Please also confirm the software that you are using (include versions / 
editions / service packs etc.).  Please specify whether you are receiving 
any error messages (or a description of what is happening on your screen 
as opposed to what is being described in the book)

Phil Perks
Technical Support Manager
Wrox Press
Message #3 by "Peter Lanoie" <planoie@n...> on Tue, 22 Oct 2002 09:23:25 -0400
I discovered something similar to this the hard way.  Trying to set the CSS
class of an HTML element.

What worked for me was

	object.Attributes.Item("class") = "myclass"

There is also:

	object.Attributes.Add("class", "myclass")

I just looked at the .Net Framework Class browser and it's reporting that
for System.Web.UI.WebControls.Label the ForeColor property is Get,Set so
it's odd that you can not set it.

Perhaps something along these lines would work:

	object.Attributes.Add("style", "color: " & strColor & ";")

It's a little awkward but it might work.

hth,

Peter


-----Original Message-----
From: John Criswell [mailto:John.Criswell@j...]
Sent: Monday, October 21, 2002 18:59
To: aspx_beginners
Subject: [aspx_beginners] Dynamically Setting the Forecolor Property in
a Web Lable Control


I have been working through the exercises in Ch 11, "Beginning ASP.NET 1.0
with C#" and I'm having difficulty with ex 3: "Bind a drop-down list to an
array containing five colors, then create a submit button that displays a
line of text in the selected color when clicked."

I am trying to set the forecolor property of an asp label control and it
turns out that it's read only.

Here is my code:

private void myDropDownList_SelectedIndexChanged(object sender,
System.EventArgs e)
		{
			string strColor;
			strColor = myDropDownList.SelectedItem.Text;
			myLabel.Text = strColor;

			switch(strColor)
			{
				case "Red":
					myLabel.ForeColor = "Red";
					break;
				case "Blue":
					myLabel.ForeColor = "Blue";
					break;
				case "Green":
					myLabel.ForeColor = "Green";
					break;
				case "Yellow":
					myLabel.ForeColor = "Yellow";
					break;
				case "Brown":
					myLabel.ForeColor = "Brown";
					break;
				default:
					myLabel.ForeColor
= "Black";
					break;
			}
		}

I'm trying to use the "SelectedIndexChanged" event of the drop down list
to change the color of the ForeColor property in the label.  The label
gets populated with the name of the color.  The text should change to the
color of the text, i.e., the string "red" should change to the color red.

This code is not working because the ForeColor property appears to be read-
only.  Is there another way to accomplish this that works?

Thanks,

John Criswell

Message #4 by Elissa Setarehshenas <elissasetareh@y...> on Tue, 22 Oct 2002 08:26:02 -0700 (PDT)
Phillip,
try this in your switch statement

myLabel.ForeColor = Color.Red; 
also include the library System.Drawing;

Hope this helps
Elissa
--- philp@w... wrote:
> > I have been working through the exercises in Ch
> 11, "Beginning ASP.NET 
> 1.0 
> w> ith C#" and I'm having difficulty with ex 3:
> "Bind a drop-down list to 
> an 
> a> rray containing five colors, then create a submit
> button that displays 
> a 
> l> ine of text in the selected color when clicked."
> 
> > I am trying to set the forecolor property of an
> asp label control and it 
> t> urns out that it's read only.
> 
> > Here is my code:
> 
> > private void
> myDropDownList_SelectedIndexChanged(object sender, 
> S> ystem.EventArgs e)
> 	> 	{
> 	> 		string strColor;
> 	> 		strColor = myDropDownList.SelectedItem.Text;
> 	> 		myLabel.Text = strColor;
> 
> > 			switch(strColor)
> 	> 		{
> 	> 			case "Red":
> 	> 				myLabel.ForeColor = "Red";
> 	> 				break;
> 	> 			case "Blue":
> 	> 				myLabel.ForeColor = "Blue";
> 	> 				break;
> 	> 			case "Green":
> 	> 				myLabel.ForeColor = "Green";
> 	> 				break;
> 	> 			case "Yellow":
> 	> 				myLabel.ForeColor = "Yellow";
> 	> 				break;
> 	> 			case "Brown":
> 	> 				myLabel.ForeColor = "Brown";
> 	> 				break;
> 	> 			default:            
> 	> 				myLabel.ForeColor 
> =>  "Black";            
> 	> 				break;
> 	> 		}
> 	> 	}
> 
> > I'm trying to use the "SelectedIndexChanged" event
> of the drop down list 
> t> o change the color of the ForeColor property in
> the label.  The label 
> g> ets populated with the name of the color.  The
> text should change to 
> the 
> c> olor of the text, i.e., the string "red" should
> change to the color red.
> 
> > This code is not working because the ForeColor
> property appears to be 
> read-
> o> nly.  Is there another way to accomplish this
> that works?
> 
> > Thanks,
> 
> > John Criswell
> 
> 
> Please note the information regarding book specific
> support questions in 
> the P2P FAQ.  I would like to explain the Wrox
> support process. If you 
> wish to directly query a problem in the book with an
> expert who knows the 
> book in detail then e-mail support@w... , with
> the title of the book 
> and the last four numbers of the ISBN in the subject
> field of the e-mail.  
> Please also confirm the software that you are using
> (include versions / 
> editions / service packs etc.).  Please specify
> whether you are receiving 
> any error messages (or a description of what is
> happening on your screen 
> as opposed to what is being described in the book)
> 
> Phil Perks
> Technical Support Manager
> Wrox Press
> ---
> Beginning ASP.NET Databases using VB.NET
> http://www.wrox.com/ACON11.asp?ISBN=1861006195
> 
> Beginning ASP.NET Databases using C#
> http://www.wrox.com/ACON11.asp?ISBN=1861007418
> 
> These books look at how we can create data-centric
> ASP.NET 
> applications. Requiring some basic knowledge of
> ASP.NET, 
> Access and SQL the authors guide you through the
> process 
> of connecting and consuming information in a variety
> of 
> ways. They are packed full of excellent illustrative
> code 
> examples, demonstrating important fundamental
> principles.


__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/

  Return to Index