Unfortunately, this is a known bug ( http://support.microsoft.com/kb/316495 ) that the GroupName property does not work properly when used in a repeater. The problem is that Repeater implements the INamingContainer interface, which requires that all nested controls have a unique name when rendering HTML. This causes the switches to break, because they must have the same name to work properly.
There are two workarounds I met:
1 - The first 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 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; }
The function should now be associated with the radio devices in the OnDataItemBound event of the repeater. Replace “RadioButton” with the name of your RadioButton control and “RadioGroup” with the GroupName of your choice:
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 that uses user control, which inherits from RadioButton. The tutorial and source code can be downloaded here: http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
Joel beckham
source share