call today Call Today 320.281.0605

Setting GroupName of RadioButton in ListView

December 5, 2010    Author: Joel

Problem: It seems there is a bug in asp.net where if you add a radio button to a control such as a ListView, Repeater, etc, the GroupName will not be the same for every RadioButton because of the asp.net naming convention adding the control hierarchy to the name.

For example I have a ListView that I include a RadioButton with the ID of “rbMorning”.   3 ListItems are dynamically created with a rendered output name of:

name="ctl00$cphMain$lvMorningSessions$ctrl0$rbMorning"
name="ctl00$cphMain$lvMorningSessions$ctrl1$rbMorning"
and
name="ctl00$cphMain$lvMorningSessions$ctrl2$rbMorning"

In this case a user would be allowed to select all 3 RadioButtons, defeating the purpose of using a RadioButton control.

Solution: Update the names with jQuery.  You will notice that that each name ends with the id of the RadioButton that you set in the ListView.  So I basically search for that and update the name to a name that all radio buttons will share.

Just a few lines of code to do this.  It all is included right here.  I am assuming you need to add jQuery to the page here as well.

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('input[name$="rbMorning"]').attr('name', 'rbMorning');
        });
    </script>

That’s it. I hope this helps someone. I am really surprised this is still an issue in asp.net 4.0 even.

Share

Hello World

March 5, 2009    Author: Joel

After almost one year of being a business I finally have launched my site and blog. It is still a work in progress but I have the main content up.

What can you expect to read on my blog?
I’ll be blogging about how I run my business, tools I use, resources I reference, and other things I encounter. I’ll also be blogging about the technology I use and of course CODE. I hope to give everyone a better idea of what all I do.

Look for some great stuff to come!

Share
Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie