A Solution for Rendering Attributes into the Individual Items of a RadioButtonList

Discussion in 'ASP .Net Web Controls' started by Dunno, May 16, 2005.

  1. Dunno

    Dunno Guest

    It's long been a problem that in some ASP.NET WebControls developers
    have not been able to render custom/additional attributes. The
    RadioButtonList is one such control. For instance you'd think that you
    could add attributes to the individual radio buttons by iterating
    through the RadioButtonLists.Items collection as so:

    foreach(ListItem item in rbl.Items)
    {
    item.Attributes.Add("custom", "custom");
    }

    No good. They attributes just don't render.

    The framework RadioButtonList implements the IRederInfoUser interface.
    I found that creating a custom RadioButtonList which derives from the
    framework one and also implements this interface can provider a
    solution to this problem.

    The only method that needs to be implemented is the RenderItem method.
    This method is responsible for rendering the individual radio buttons
    in the list. Example implementation below.

    public class CustomRadioButtonList : RadioButtonList, IRepeatInfoUser
    {
    void IRepeatInfoUser.RenderItem(ListItemType itemType, int
    repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
    RadioButton radioButton = new RadioButton();
    radioButton.Page = this.Page;
    radioButton.GroupName = this.UniqueID;
    radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
    radioButton.Text = this.Items[repeatIndex].Text;
    radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
    radioButton.Checked = this.Items[repeatIndex].Selected;
    radioButton.TextAlign = this.TextAlign;
    radioButton.AutoPostBack = this.AutoPostBack;
    radioButton.TabIndex = this.TabIndex;
    radioButton.Enabled = this.Enabled;

    // Add custom attributes here.
    writer.AddAttribute("custom", "custom");

    radioButton.RenderControl(writer);
    }
    }

    It may not be the most elegant way around this problem but it has
    worked for me.

    Happy Coding.
    Dunno, May 16, 2005
    #1
    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. Mike S
    Replies:
    1
    Views:
    1,062
    Mike S
    Jul 15, 2003
  2. Karl
    Replies:
    0
    Views:
    759
  3. raz
    Replies:
    0
    Views:
    354
  4. raz
    Replies:
    0
    Views:
    372
  5. W Akthar

    Inserting individual items into a datagrid

    W Akthar, Aug 10, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    91
    Bharat Biyani
    Aug 11, 2004
Loading...

Share This Page