Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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" />
  <Columns>
      <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
      <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
      <asp:BoundField DataField="Description" HeaderText="Description" />
      <asp:TemplateField HeaderText="Select One">
        <ItemTemplate>
          <input name="MyRadioButton" type="radio" 
                    value='<%# Eval("CategoryID") %>' />
        </ItemTemplate>
      </asp:TemplateField>
  </Columns>
  <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" />
</asp:GridView>

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!

License

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

Share

About the Author

azamsharp
Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.
 
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.
 
HighOnCoding.com
Blog

 

Buy my iPhone app ABC Pop

Comments and Discussions

 
QuestionDisabling a radiobutton selection PinmemberDan Roma14-Jan-14 5:11 
GeneralMy vote of 2 PinmemberSravan S5-Aug-13 18:58 
GeneralMy vote of 4 PinmemberAlireza_136210-Apr-13 17:27 
GeneralMy vote of 5 Pinmemberlk.934620-Mar-13 23:06 
QuestionCode for Vb,Net PinmemberPriyanka Jain22-Aug-12 20:07 
GeneralMy vote of 5 PinmemberĐăng Đau Đớn5-Aug-12 9:33 
QuestionHow do i get the Row Index that I selected? PinmemberFranco Cipriano30-May-12 9:59 
AnswerRe: How do i get the Row Index that I selected? PinmemberAndres Cichero31-May-12 11:41 
QuestionAdd Panel PinmemberElham M2-Sep-11 23:09 
Hi
I have grid view that bind to database which is have ID,dat ,Body ,Status,Title,UserName field I want to have an edit column that when I click on it show a panel in status column that show data in body field to edit this field I think you can help me more. Thanks
GeneralMy vote of 4 PinmemberSyed Wajahat23-Feb-11 3:41 
GeneralHope it doesn't work Pinmembermohanjune198713-Sep-10 16:13 
GeneralGetRowIndex method PinmemberLufty88984-Apr-10 14:52 
GeneralMy vote is: poor.... PinmemberMember 30736892-Jan-09 9:02 
GeneralMy vote of 1 PinmemberMember 30736892-Jan-09 9:00 
GeneralMy vote of 2 PinmemberGeoffrey Morton-Haworth14-Dec-08 6:13 
GeneralUsing a literal is a better approach PinmemberGeoffrey Morton-Haworth13-Dec-08 6:32 
GeneralRe: Using a literal is a better approach Pinmemberazamsharp13-Dec-08 13:40 
GeneralRe: Using a literal is a better approach PinmemberGeoffrey Morton-Haworth14-Dec-08 6:06 
GeneralAnother workaround... PinmemberSmuus26-Nov-08 6:12 
QuestionQuestion Pinmemberharicosmo21-Aug-08 6:54 
GeneralQuery PinmemberMember 174326212-Dec-07 0:40 
Generalmaintain the state of clientside radiobutton PinmemberMember 409133810-Dec-07 20:30 
GeneralGood Pinmembervp200030-Nov-07 15:56 
GeneralAnother way PinmemberReticulatingSplines14-Sep-07 5:01 
QuestionHow Can I get the checked property of the radioButtons from a gridView PinmemberMember #381065813-Feb-07 9:38 
AnswerRe: How Can I get the checked property of the radioButtons from a gridView Pinmemberazamsharp13-Feb-07 9:53 
GeneralMemorize checked row after postback PinmemberJL. ROBERT12-Feb-07 21:29 
GeneralRe: Memorize checked row after postback Pinmemberazamsharp20-Feb-07 16:28 
QuestionGetting position in gridview Pinmemberpablo luengo13-Dec-06 22:07 
QuestionRadio Button in GridView Pinmemberdotnetguru20022-Nov-06 5:27 
GeneralSooooooo basic PinmemberBritney S. Morales31-Oct-06 9:35 
GeneralJust needed one more thing Pinmemberjoshnorman3224-Oct-06 2:52 
GeneralThis is a great solution Pinmemberlmcguffee22-Jul-06 8:49 
GeneralThanks Pinmembersantosh poojari17-Jul-06 21:01 
GeneralCooool PinmemberFayez Mutairi31-May-06 7:52 
GeneralDoesn't seem to work in real world scenario: Pinmemberdirc11-Apr-06 22:09 
GeneralThis is the most basic thing PinmemberAlomgir Miah13-Feb-06 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140922.1 | Last Updated 13 Feb 2006
Article Copyright 2006 by azamsharp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid