Click here to Skip to main content
11,926,404 members (63,826 online)
Click here to Skip to main content
Add your own
alternative version


58 bookmarked

RadioButtons inside a GridView control

, 13 Feb 2006
Rate this:
Please Sign up or sign in to vote.
In this article, we will see how to use RadioButtons inside a GridView control and retrieve the values associated with them.


In one of my articles, I discussed about how you to get the values of the selected checkboxes that are inside a GridView control. In this article, I will show you how you can use RadioButtons inside a GridView control.

Big deal about radio buttons

You must be wondering what is the big deal about it since I can simply place a RadioButton server control inside a GridView and retrieve the value of the selected row just like I did for checkboxes. Well, RadioButtons are different since you can select only one of them at a time. Then you must be thinking, so what if I give the name to the RadioButtons and won't that take care of the problem. Not exactly, since GridView is rendered as several rows and each row has an ID. Now, if RadioButtons are rendered inside a GridView control then the GridView control will assign them a name at runtime which will be different.

The effect will be that you will be able to select multiple RadioButtons. So, lets see in this article how we can solve this problem.

You should also check out "Selecting multiple checkboxes inside a GridView control".

Using the HTML RadioButton

The solution is pretty simple. Just use HTML radio buttons instead of RadioButton server controls. Let's see how this is done. First of all, you need to add a template column in the GridView control which will contain the radio button.

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" BackColor="White"
        BorderColor="#CC9966" BorderStyle="None" 
        BorderWidth="1px" CellPadding="4" Font-Names="Verdana">
  <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
      <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
      <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
      <asp:BoundField DataField="Description" HeaderText="Description" />
      <asp:TemplateField HeaderText="Select One">
          <input name="MyRadioButton" type="radio" 
                    value='<%# Eval("CategoryID") %>' />
  <RowStyle BackColor="White" ForeColor="#330099" />
  <SelectedRowStyle BackColor="#FFCC66" 
          Font-Bold="True" ForeColor="#663399" />
  <PagerStyle BackColor="#FFFFCC" 
          ForeColor="#330099" HorizontalAlign="Center" />
  <HeaderStyle BackColor="#990000" 
          Font-Bold="True" ForeColor="#FFFFCC" />

I have made the template column bold so it will be easier for you to identify. I have given the name "MyRadioButton" to the HTML radio button. Since, I want the primary key of the row when the radio button is checked, I have bound it to the value of the CategoryID field.

Let's see a picture so that you know what is going on:

As you can also see, there is a Button control on the form. Now, when I press the button I want to get the CategoryID of the row whose radio button is checked. This is pretty easy, check out the code below:

protected void Button1_Click(object sender, EventArgs e)
  string selectedValue = Request.Form["MyRadioButton"];
  lblMsg.Text = selectedValue;

Request.Form takes the name of the control, in this case "MyRadioButton", and returns the value back to the variable.

I hope you liked this article, happy coding!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, and

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

My Blog:


Buy my iPhone app ABC Pop

You may also be interested in...

Comments and Discussions

QuestionDisabling a radiobutton selection Pin
Dan Roma14-Jan-14 6:11
memberDan Roma14-Jan-14 6:11 
GeneralMy vote of 2 Pin
Sravan S5-Aug-13 19:58
memberSravan S5-Aug-13 19:58 
GeneralMy vote of 4 Pin
Alireza_136210-Apr-13 18:27
memberAlireza_136210-Apr-13 18:27 
GeneralMy vote of 5 Pin
lk.934621-Mar-13 0:06
memberlk.934621-Mar-13 0:06 
QuestionCode for Vb,Net Pin
Priyanka Jain22-Aug-12 21:07
memberPriyanka Jain22-Aug-12 21:07 
GeneralMy vote of 5 Pin
Đăng Đau Đớn5-Aug-12 10:33
memberĐăng Đau Đớn5-Aug-12 10:33 
QuestionHow do i get the Row Index that I selected? Pin
Franco Cipriano30-May-12 10:59
memberFranco Cipriano30-May-12 10:59 
I tried this but it gives me the values in the cell. How to I get the row index so that I can use it to reference on the on the other columns?

AnswerRe: How do i get the Row Index that I selected? Pin
Andres Cichero31-May-12 12:41
memberAndres Cichero31-May-12 12:41 
QuestionAdd Panel Pin
Elham M3-Sep-11 0:09
memberElham M3-Sep-11 0:09 
GeneralMy vote of 4 Pin
Syed Wajahat23-Feb-11 4:41
memberSyed Wajahat23-Feb-11 4:41 
GeneralHope it doesn't work Pin
mohanjune198713-Sep-10 17:13
membermohanjune198713-Sep-10 17:13 
GeneralGetRowIndex method Pin
Lufty88984-Apr-10 15:52
memberLufty88984-Apr-10 15:52 
GeneralMy vote is: poor.... Pin
Member 30736892-Jan-09 10:02
memberMember 30736892-Jan-09 10:02 
GeneralMy vote of 1 Pin
Member 30736892-Jan-09 10:00
memberMember 30736892-Jan-09 10:00 
GeneralMy vote of 2 Pin
Geoffrey Morton-Haworth14-Dec-08 7:13
memberGeoffrey Morton-Haworth14-Dec-08 7:13 
GeneralUsing a literal is a better approach Pin
Geoffrey Morton-Haworth13-Dec-08 7:32
memberGeoffrey Morton-Haworth13-Dec-08 7:32 
GeneralRe: Using a literal is a better approach Pin
azamsharp13-Dec-08 14:40
memberazamsharp13-Dec-08 14:40 
GeneralRe: Using a literal is a better approach Pin
Geoffrey Morton-Haworth14-Dec-08 7:06
memberGeoffrey Morton-Haworth14-Dec-08 7:06 
GeneralAnother workaround... Pin
Smuus26-Nov-08 7:12
memberSmuus26-Nov-08 7:12 
QuestionQuestion Pin
haricosmo21-Aug-08 7:54
memberharicosmo21-Aug-08 7:54 
GeneralQuery Pin
Member 174326212-Dec-07 1:40
memberMember 174326212-Dec-07 1:40 
Generalmaintain the state of clientside radiobutton Pin
Member 409133810-Dec-07 21:30
memberMember 409133810-Dec-07 21:30 
GeneralGood Pin
vp200030-Nov-07 16:56
membervp200030-Nov-07 16:56 
GeneralAnother way Pin
ReticulatingSplines14-Sep-07 6:01
memberReticulatingSplines14-Sep-07 6:01 
QuestionHow Can I get the checked property of the radioButtons from a gridView Pin
Member #381065813-Feb-07 10:38
memberMember #381065813-Feb-07 10:38 
AnswerRe: How Can I get the checked property of the radioButtons from a gridView Pin
azamsharp13-Feb-07 10:53
memberazamsharp13-Feb-07 10:53 
GeneralMemorize checked row after postback Pin
JL. ROBERT12-Feb-07 22:29
memberJL. ROBERT12-Feb-07 22:29 
GeneralRe: Memorize checked row after postback Pin
azamsharp20-Feb-07 17:28
memberazamsharp20-Feb-07 17:28 
QuestionGetting position in gridview Pin
pablo luengo13-Dec-06 23:07
memberpablo luengo13-Dec-06 23:07 
QuestionRadio Button in GridView Pin
dotnetguru20022-Nov-06 6:27
memberdotnetguru20022-Nov-06 6:27 
GeneralSooooooo basic Pin
Britney S. Morales31-Oct-06 10:35
memberBritney S. Morales31-Oct-06 10:35 
GeneralJust needed one more thing Pin
joshnorman3224-Oct-06 3:52
memberjoshnorman3224-Oct-06 3:52 
GeneralThis is a great solution Pin
lmcguffee22-Jul-06 9:49
memberlmcguffee22-Jul-06 9:49 
GeneralThanks Pin
santosh poojari17-Jul-06 22:01
membersantosh poojari17-Jul-06 22:01 
GeneralCooool Pin
<marquee>Fayez Mutairi</marquee>31-May-06 8:52
memberFayez Mutairi31-May-06 8:52 
GeneralDoesn't seem to work in real world scenario: Pin
dirc11-Apr-06 23:09
memberdirc11-Apr-06 23:09 
GeneralThis is the most basic thing Pin
Alomgir Miah13-Feb-06 9:27
memberAlomgir Miah13-Feb-06 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 13 Feb 2006
Article Copyright 2006 by azamsharp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid