ASP.NET RadioButton messing with the name (groupname) ASP.NET RadioButton messing with the name (groupname) asp.net asp.net

ASP.NET RadioButton messing with the name (groupname)


ASP.NET Tip: Using RadioButton Controls in a Repeater

This is the code for the JavaScript function:

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;}

The code is linked to the Repeater through the ItemDataBound event. For it to work properly, you need to know the name of the Repeater control, as well as the GroupName you're assigning to the RadioButtons. In this case, I'm using rptPortfolios as the name of the Repeater, and Portfolios as the group name:

protected void rptPortfolios_ItemDataBound(object sender,                                           RepeaterItemEventArgs e){   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType      != ListItemType.AlternatingItem)      return;   RadioButton rdo = (RadioButton)e.Item.FindControl("rdoSelected");   string script =      "SetUniqueRadioButton('rptPortfolios.*Portfolios',this)";   rdo.Attributes.Add("onclick", script);}

REF: http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c12371/


Google-fu: asp.net radiobutton repeater problem

Indeed an unfortunate consequence of the id mangling. My take would be creating a - or picking one of the many available - custom control that adds support for same name on the client.


Vladimir Smirnov has already created a great custom control that resolves this issue. We have been using the GroupRadioButton in our projects and it has been working perfectly with radio buttons created inside of a repeater and others outside the repeater all being a part of the same group.