ASP.NET bug with RadioButton GroupName in Repeater

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

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

Computers & Internet Web Development Website Hosting

500 Internal Server Error after migrating from IIS 7.5 to IIS 10

by John Avis | November 4, 2019

As support ends for Microsoft Windows Server 2008 I have recently gone through migrating some websites to a new server running Windows Server 2016 and IIS 10 but some of the websites did not work.


Jquery/Javascript Web Development

tagInput: A simple jQuery plugin for tag entry using Bootstrap 4

by John Avis | October 15, 2019

For a website project I needed a way to enter multiple tags. I just wanted something simple that I could easily modify to suit my own needs, so I wrote my own.


ASP.NET Web Forms Bootstrap Web Development

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.

Comments

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

Leave a Comment

Tags

About me

...random postings about web development and programming, Internet, computers, electronics and automotive topics.

Subscribe

Get the latest posts delivered to your inbox.