consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

A Coder's Notes

Email :
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull

Get connected and Online Tottington
remote management Makerfield code and design Aspull

ASP.NET - Radio Buttons In Repeaters - SOLVED! - 03/01/2011

I like the asp:repeater. Overall I prefer it to the asp:datagrid method. It does require more code but it allows greater control of the HTML. That's just my opinion, it's up to you what you prefer.

There is a known bug in the asp:repeater and the asp:datagrid methods. That of the radio button.

If you use a repeater the recommended method is as follows...

<form runat = "server">

<asp:repeater id = "myrepeater" runat = "server">

<asp:radiobutton id = "myradiobutton"
runat = "server"
text = '<%# container.dataitem("mydata") %>'
groupname = "mygroup"
value = '<%# container.dataitem("mydata") %>'



The problem is the code it generates.

<input id="myrepeater_ctl00_myradiobutton" type="radio" name="myrepeater$ctl00$mygroup" value="100" /><label for="myrepeater_ctl00_myradiobutton">100</label>

<input id="myrepeater_ctl01_myradiobutton" type="radio" name="myrepeater$ctl01$mygroup" value="101" /><label for="myrepeater_ctl01_myradiobutton">101</label>

<input id="myrepeater_ctl02_myradiobutton" type="radio" name="myrepeater$ctl02$mygroup" value="102" /><label for="myrepeater_ctl02_myradiobutton">102</label>

<input id="myrepeater_ctl03_myradiobutton" type="radio" name="myrepeater$ctl03$mygroup" value="103" /><label for="myrepeater_ctl03_myradiobutton">103</label>

<input id="myrepeater_ctl04_myradiobutton" type="radio" name="myrepeater$ctl04$mygroup" value="104" /><label for="myrepeater_ctl04_myradiobutton">104</label>

<input id="myrepeater_ctl05_myradiobutton" type="radio" name="myrepeater$ctl05$mygroup" value="105" /><label for="myrepeater_ctl05_myradiobutton">105</label>

Now, for those of you who understand the radio button in good old plain HTML, you will see the problem here. Each of these radio buttons has a DIFFERENT id and name. That means that there are lots of unique raio buttons, each of which is selectable. Radio buttons are designed such that each group of radio buttons has the SAME id and name and only the VALUE should be different.

This means that as above each radio button is selectable, whereas the purpose of a radio button is that ONLY ONE should be selectable.

This is indeed a known issue and microsoft documents this at the following page...;en-us;Q316495

If you search for information about this on the web then the most common solution is to use a piece of Javascript to go through the ids and names to get the client browser to correct the problem and re-name all the buttons correctly. Of course this is a workaround, and for my money something of a hack job. Why do techies like to make things so complicated!!

I believe there is a much simpler solution.

<form runat = "server">

<asp:repeater id = "myrepeater" runat = "server">

<input type = "radio" name = "myradiobutton" value = "<%# container.dataitem("mydata") %>" />



First of all, the code is a lot shorter! All we are doing here is slipping in a standard HTML radio button, and using the repeater to change ONLY the value of each radio button.

Now ASP.NET script kiddies who can't think for themselves will be getting into a sweat here. If the ASP code has not generated the the radio button then how the devil can we get the data, the value, of the selected radio button. Come on kids...

<script runat = "server">

sub mysub(sender as object, e as eventargs)
  dim radiobuttonvalue as string

  radiobuttonvalue = request("myradiobutton")
end sub


It's really not hard. When the page is posted back we use the traditional way of collecting the radio button's value...request("radio_button_name")


Post A Comment

Name Comment
Liri Thanks!!! Works!!!
Emerson Yes, this is pure genius. THANK YOU!
Hadi Thank you so much, it really helped me :)
Emily Wow thanks
Nick Not Helpfull
siva hello
Ziad What if we need a postback for OnCheckedChanged
esha how to do it with c# coding
Jean Doesn't work on 4.6
man Not Helpfull:(
Marat Same problem with Name
Dudeguy 15/02-17 Still works like a charm! Thanks a ton! Much better than having to dabble into filthy javascript or Jquery.
programming services specialist
Valid XHTML 1.0 Transitional