ASP.NET bug with RadioButton GroupName in Repeater

John Avis by | September 1, 2019 | ASP.NET Web Forms

I rediscovered a bug in ASP.NET that affects RadioButtons inside repeaters. Here is my solution to the problem.
I was recently working on an ASP.NET Web Forms project where I needed to create a group of RadioButtons with a Repeater.

I rediscovered a bug in ASP.NET that results in each RadioButton having a unique GroupName which means they all operate independently.

Of course I attempted to take the easy route of Google-ing the problem and I found many discussions and posts about this issue. I looked at and tried some of the suggested workarounds but didn't like some and others didn't work.

Inspired by some JavaScript solutions that simply deselect all other RadioButtons in the same group, I put together the following simple jQuery script to do the same.

$(function(){
$('[name$="$RadioButton1"]').click(function(){
var clientId=$(this).attr('id');
$('[name$="$RadioButton1"]').each(function(){
if($(this).attr('id') != clientId){
$(this).prop('checked',false);
}
});
});
});

You need to replace "RadioButton1" with the ID of your RadioButton control in the two places in the script.

I added this script in the code-behind file in the Page_PreRender event when it was needed, like this:

Page.ClientScript.RegisterStartupScript(this.GetType(), "RadioButton1", "$(function(){$('[name$="$RadioButton1"]').click(function(){var clientId=$(this).attr('id');$('[name$="$RadioButton1"]').each(function(){if($(this).attr('id')!=clientId){$(this).prop('checked',false);}});});});", true);

I know this script could be improved a lot, but as is often the case, as soon as my prototype worked as required I left it as is and moved on to other priorities. No time for optimisation.

asp-net-radiobutton-in-repeater-issue.jpg

[A Google search shows many people have faced this problem... Wonder why it hasn't been fixed?]

Related Posts

ASP.NET Web Forms Bootstrap

More on Bootstrap 4 modals in ASP.NET Web Forms applications using UpdatePanels

by John Avis | April 9, 2019

In this, my third post on this topic, I go over the methods I am using now and the problems I have encountered and overcome.


ASP.NET Web Forms Classic ASP

Custom error pages for ASP.NET Web Forms and Classic ASP in IIS 7 and 8.5

by John Avis | February 20, 2019

I was recently doing some work on a website which has a mixture of older Classic ASP pages and ASP.NET Web Forms pages and ran into problems with custom error pages.


ASP.NET Web Forms

Nicer localised UpdateProgress’ for your ASP.NET pages with multiple UpdatePanels

by John Avis | February 9, 2018

On an ASP.NET Web Forms page with multiple UpdatePanels it’s nice to give the user some feedback when one of the panels is being updated.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

Tags

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

Subscribe

Get the latest posts delivered to your inbox.