ListItem color change - RB

M

mg

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>
 
A

alex bowers

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
 
G

Guest

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>



.
.
 
A

alex bowers

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;

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>



.
.
.
 
M

mg

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;

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>



.

.
.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top