Using ASP.NET Radio Buttons in Repeater

There is a bug with using radio buttons in a repeater control, in that they are not mutually exclusive when you run the ASP.NET page, even when you specify attribute GroupName: http://support.microsoft.com/kb/316495.






The workaround, as suggested by Microsoft Support, is by adding the following JavaScript function to the head section of the aspx page, …

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

and a client-side onclick event to the radio button set in the aspx.cs file:

protected void repeaterName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var rbt = (RadioButton)e.Item.FindControl("radioButtonName");
var setUniqueRadioButton = "SetUniqueRadioButton('repeaterName.*radioButtonGroupName', this);";
rbt.Attributes.Add("onclick", setUniqueRadioButton);
}
}
Advertisements

One thought on “Using ASP.NET Radio Buttons in Repeater

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s