Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

radio button list in repeater control

Tags:

asp.net

I have a repeater control in my page. I need to have a radio button in all the rows(Item template) on checking an radio button the remaining radio buttons must unchecked.

How to do this?

Thanks in advance, Tor

like image 714
tor Avatar asked Oct 27 '10 05:10

tor


People also ask

What is the difference between radio button control and radio button list control?

You can get the selected Index in RadioButtonList as it work on collection of ListItem. In contrast, the RadioButtonList control is a single control that acts as a parent control for a collection of radio button list items.

What is radio button list in asp net?

The DevExpress ASP.NET Radio Button List (ASPxRadioButtonList) editor is a radio button group that allows end-users to select a single item at a time. Its contents can be generated dynamically by binding the editor to a data source.

What is Repeater control?

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. Repeater is a Data Bind Control. Data Bind Controls are container controls.


1 Answers

Unfortunately, it's a known bug ( http://support.microsoft.com/kb/316495 ) that the GroupName property doesn't work as expected when used in a Repeater. The problem is that the Repeater implements the INamingContainer interface which requires all nested controls to have a unique name when rendered out to HTML. This causes the radio buttons to break because in order for them to work properly they must have identical names.

There are 2 work-arounds that I've come across:

1 - The first is a client-side javascript solution. It was provided by Microsoft support. Or an easier to read version here. The instructions are as follows. Include the following javascript in the HEAD:

function SetUniqueRadioButton(nameregex, current)
{
      re = new RegExp(nameregex);
      for(i = 0; i < document.forms[0].elements.length; i++)
      {
            elm = document.forms[0].elements[i]
            if (elm.type == 'radio')
            {
                  if (re.test(elm.name))
                  {
                          elm.checked = false;
                  }
             }
      }
      current.checked = true;
}

Now the function needs to be linked to the Radio Buttons in the OnDataItemBound event of the repeater. Replace "RadioButton" with the name of your RadioButton control and "RadioGroup" with the GroupName you have chosen:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
      if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
      RadioButton rb = (RadioButton) e.Item.FindControl("RadioButton");
      string script = "SetUniqueRadioButton('Repeater1.*RadioGroup',this)";
      rb.Attributes.Add("onclick", script);
}

2 - The second solution is a server-side solution using a custom usercontrol that inherits from RadioButton. The tutorial and source code can be downloaded here: http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx

like image 124
Joel Beckham Avatar answered Nov 02 '22 19:11

Joel Beckham