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.

[A Google search shows many people have faced this problem... Wonder why it hasn't been fixed?]
Rate this post:
Comments
There are no comments yet. Be the first to leave a comment!