Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ListItem color change - RB

Reply
Thread Tools

ListItem color change - RB

 
 
mg
Guest
Posts: n/a
 
      11-11-2003
How can I change the color of the display text beside a
single ListItem of a RadioButtonList when the associated
radiobutton is selected - hopefully using C# in the
codebehind of a WebForm?

<asp:RadioButtonList id="RadioButtonList1" ........ >
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
<asp:ListItem Value="Maybe">Maybe</asp:ListItem>
</asp:RadioButtonList>



 
Reply With Quote
 
 
 
 
alex bowers
Guest
Posts: n/a
 
      11-11-2003
Hi
You can access the ListItem.Attributes collection on the
server to change it's color.
eg

[C#]
foreach (ListItem li in RadioButtonList1.Items)
{
if (li.Selected)
li.Attributes["color"] = "red";
}

not tested, but should work.

alex
>-----Original Message-----
>How can I change the color of the display text beside a
>single ListItem of a RadioButtonList when the associated
>radiobutton is selected - hopefully using C# in the
>codebehind of a WebForm?
>
><asp:RadioButtonList id="RadioButtonList1" ........ >
><asp:ListItem Value="Yes">Yes</asp:ListItem>
><asp:ListItem Value="No">No</asp:ListItem>
><asp:ListItem Value="Maybe">Maybe</asp:ListItem>
></asp:RadioButtonList>
>
>
>
>.
>

 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      11-11-2003
Alex,

I had no luck with the following code:

private void RadioButtonList1_SelectedIndexChanged( ...
{
foreach (ListItem li in RadioButtonList1.Items)
{
if (li.Selected==true)
{
li.Attributes["ForeColor"] = "red";
}
}

Any further thoughts?

mg
>-----Original Message-----
>Hi
>You can access the ListItem.Attributes collection on the
>server to change it's color.
>eg
>
>[C#]
>foreach (ListItem li in RadioButtonList1.Items)
>{
> if (li.Selected)
> li.Attributes["color"] = "red";
>}
>
>not tested, but should work.
>
>alex
>>-----Original Message-----
>>How can I change the color of the display text beside a
>>single ListItem of a RadioButtonList when the associated
>>radiobutton is selected - hopefully using C# in the
>>codebehind of a WebForm?
>>
>><asp:RadioButtonList id="RadioButtonList1" ........ >
>><asp:ListItem Value="Yes">Yes</asp:ListItem>
>><asp:ListItem Value="No">No</asp:ListItem>
>><asp:ListItem Value="Maybe">Maybe</asp:ListItem>
>></asp:RadioButtonList>
>>
>>
>>
>>.
>>

>.
>

 
Reply With Quote
 
alex bowers
Guest
Posts: n/a
 
      11-12-2003
mg,
I couldn't get anything to happen on the server-side
either, but managed to write some client-side script to
achieve the color change (and also modify any of the
radio buttons attributes too).
Assume your form is called "Form1" and your
radiolist "RadioButtonList1", and that you have 2
elements in the list.
First modify the <BODY> tag in your HTML to read <BODY
onload = 'assign_events();'>
then add the following javascript functions. This is
kind of clunky, and can be simplifed by writing out the
client scripts from server side page.RegisterClientScript
calls.

alex


function assign_events() {


document.all("RadioButtonList1_0").onclick =
RadioButtonList1_2_onclick;

document.all("RadioButtonList1_1").onclick =
RadioButtonList1_1_onclick;

}

function RadioButtonList1_0_onclick() {
dostuff(this);
}

function RadioButtonList1_1_onclick() {
dostuff(this);
}

function dostuff(item)
{
for (i=0;i<document.Form1.length;i++)
{
Obj = document.Form1.elements[i];

if (Obj.type == 'radio')
{

Obj.nextSibling.style.color = "black";
}
}
item.nextSibling.style.color = "red";



}
>-----Original Message-----
>Alex,
>
>I had no luck with the following code:
>
>private void RadioButtonList1_SelectedIndexChanged( ...
>{
>foreach (ListItem li in RadioButtonList1.Items)
>{
>if (li.Selected==true)
>{
>li.Attributes["ForeColor"] = "red";
>}
>}
>
>Any further thoughts?
>
>mg
>>-----Original Message-----
>>Hi
>>You can access the ListItem.Attributes collection on

the
>>server to change it's color.
>>eg
>>
>>[C#]
>>foreach (ListItem li in RadioButtonList1.Items)
>>{
>> if (li.Selected)
>> li.Attributes["color"] = "red";
>>}
>>
>>not tested, but should work.
>>
>>alex
>>>-----Original Message-----
>>>How can I change the color of the display text beside

a
>>>single ListItem of a RadioButtonList when the

associated
>>>radiobutton is selected - hopefully using C# in the
>>>codebehind of a WebForm?
>>>
>>><asp:RadioButtonList id="RadioButtonList1" ........ >
>>><asp:ListItem Value="Yes">Yes</asp:ListItem>
>>><asp:ListItem Value="No">No</asp:ListItem>
>>><asp:ListItem Value="Maybe">Maybe</asp:ListItem>
>>></asp:RadioButtonList>
>>>
>>>
>>>
>>>.
>>>

>>.
>>

>.
>

 
Reply With Quote
 
mg
Guest
Posts: n/a
 
      11-12-2003
Alex,

That did the job. Thanks!

mg

P.S. I changed

RadioButtonList1_2_onclick; to RadioButtonList1_0_onclick;

>-----Original Message-----
>mg,
>I couldn't get anything to happen on the server-side
>either, but managed to write some client-side script to
>achieve the color change (and also modify any of the
>radio buttons attributes too).
>Assume your form is called "Form1" and your
>radiolist "RadioButtonList1", and that you have 2
>elements in the list.
>First modify the <BODY> tag in your HTML to read <BODY
>onload = 'assign_events();'>
>then add the following javascript functions. This is
>kind of clunky, and can be simplifed by writing out the
>client scripts from server side page.RegisterClientScript
>calls.
>
>alex
>
>
>function assign_events() {
>
>
>document.all("RadioButtonList1_0").onclick =
>RadioButtonList1_2_onclick;
>
>document.all("RadioButtonList1_1").onclick =
>RadioButtonList1_1_onclick;
>
> }
>
> function RadioButtonList1_0_onclick() {
> dostuff(this);
>}
>
> function RadioButtonList1_1_onclick() {
>dostuff(this);
>}
>
>function dostuff(item)
>{
> for (i=0;i<document.Form1.length;i++)
> {
> Obj = document.Form1.elements[i];
>
> if (Obj.type == 'radio')
> {
>
> Obj.nextSibling.style.color = "black";
> }
> }
> item.nextSibling.style.color = "red";
>
>
>
>}
>>-----Original Message-----
>>Alex,
>>
>>I had no luck with the following code:
>>
>>private void RadioButtonList1_SelectedIndexChanged( ...
>>{
>>foreach (ListItem li in RadioButtonList1.Items)
>>{
>>if (li.Selected==true)
>>{
>>li.Attributes["ForeColor"] = "red";
>>}
>>}
>>
>>Any further thoughts?
>>
>>mg
>>>-----Original Message-----
>>>Hi
>>>You can access the ListItem.Attributes collection on

>the
>>>server to change it's color.
>>>eg
>>>
>>>[C#]
>>>foreach (ListItem li in RadioButtonList1.Items)
>>>{
>>> if (li.Selected)
>>> li.Attributes["color"] = "red";
>>>}
>>>
>>>not tested, but should work.
>>>
>>>alex
>>>>-----Original Message-----
>>>>How can I change the color of the display text beside

>a
>>>>single ListItem of a RadioButtonList when the

>associated
>>>>radiobutton is selected - hopefully using C# in the
>>>>codebehind of a WebForm?
>>>>
>>>><asp:RadioButtonList id="RadioButtonList1" ........ >
>>>><asp:ListItem Value="Yes">Yes</asp:ListItem>
>>>><asp:ListItem Value="No">No</asp:ListItem>
>>>><asp:ListItem Value="Maybe">Maybe</asp:ListItem>
>>>></asp:RadioButtonList>
>>>>
>>>>
>>>>
>>>>.
>>>>
>>>.
>>>

>>.
>>

>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<asp:ListItem Value="0">blank choice</asp:ListItem> ? Jim in Arizona ASP .Net 7 11-22-2006 08:31 AM
ropdownlist listitem.value not working - returns listitem.text instead Mad Scientist Jr ASP .Net 2 06-29-2004 01:13 PM
How do I change a listitem font color? Do ASP .Net 1 12-08-2003 05:58 PM
Changing background-color of a ListItem Tom ASP .Net 0 12-03-2003 02:31 PM
ListItem color change - RB mg ASP .Net Web Controls 1 11-11-2003 09:59 PM



Advertisments