ListItem color change - RB

Discussion in 'ASP .Net' started by mg, Nov 11, 2003.

  1. mg

    mg Guest

    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>
    mg, Nov 11, 2003
    #1
    1. Advertising

  2. mg

    alex bowers Guest

    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>
    >
    >
    >
    >.
    >
    alex bowers, Nov 11, 2003
    #2
    1. Advertising

  3. mg

    Guest 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>
    >>
    >>
    >>
    >>.
    >>

    >.
    >
    Guest, Nov 11, 2003
    #3
  4. mg

    alex bowers Guest

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

    >>.
    >>

    >.
    >
    alex bowers, Nov 12, 2003
    #4
  5. mg

    mg Guest

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

    >>.
    >>

    >.
    >
    mg, Nov 12, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Do
    Replies:
    1
    Views:
    8,517
    Munsifali Rashid
    Dec 8, 2003
  2. Mad Scientist Jr
    Replies:
    2
    Views:
    438
    Mad Scientist Jr
    Jun 29, 2004
  3. Jim in Arizona
    Replies:
    7
    Views:
    913
    Badass Scotsman
    Nov 22, 2006
  4. mg

    ListItem color change - RB

    mg, Nov 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    140
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    411
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page