ASP.NET bug with RadioButton GroupName in Repeater

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

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

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Web Development

Intermittent "Unable to read data from the transport connection: net_io_connectionclosed" errors

by johna | May 6, 2020
If you are having intermittent problems sending email in .NET using System.Net.Mail consider switching libraries.

Comments

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

Leave a Comment

About

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

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.