Click here to Skip to main content
6,818,958 members and growing! (20,431 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

RadioButtons inside a GridView control

By azamsharp

In this article, we will see how to use RadioButtons inside a GridView control and retrieve the values associated with them.
C#, Windows, .NET, ASP.NET, Visual-Studio, WebForms, Dev
Posted:13 Feb 2006
Views:104,676
Bookmarked:45 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
33 votes for this article.
Popularity: 5.91 Rating: 3.89 out of 5
6 votes, 18.2%
1
1 vote, 3.0%
2

3
9 votes, 27.3%
4
17 votes, 51.5%
5

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

About the Author

azamsharp


Member
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 www.HighOnCoding.com


My Blog:

Blog

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralMy vote is: poor.... PinmemberMember 307368910:02 2 Jan '09  
GeneralMy vote of 1 PinmemberMember 307368910:00 2 Jan '09  
GeneralMy vote of 2 PinmemberGeoffrey Morton-Haworth7:13 14 Dec '08  
GeneralUsing a literal is a better approach PinmemberGeoffrey Morton-Haworth7:32 13 Dec '08  
GeneralRe: Using a literal is a better approach Pinmemberazamsharp14:40 13 Dec '08  
GeneralRe: Using a literal is a better approach PinmemberGeoffrey Morton-Haworth7:06 14 Dec '08  
GeneralAnother workaround... PinmemberSmuus7:12 26 Nov '08  
QuestionQuestion Pinmemberharicosmo7:54 21 Aug '08  
GeneralQuery PinmemberMember 17432621:40 12 Dec '07  
Generalmaintain the state of clientside radiobutton PinmemberMember 409133821:30 10 Dec '07  
GeneralGood Pinmembervp200016:56 30 Nov '07  
GeneralAnother way PinmemberReticulatingSplines6:01 14 Sep '07  
QuestionHow Can I get the checked property of the radioButtons from a gridView Pinmember10:38 13 Feb '07  
AnswerRe: How Can I get the checked property of the radioButtons from a gridView Pinmemberazamsharp10:53 13 Feb '07  
GeneralMemorize checked row after postback PinmemberJL. ROBERT22:29 12 Feb '07  
GeneralRe: Memorize checked row after postback Pinmemberazamsharp17:28 20 Feb '07  
QuestionGetting position in gridview Pinmemberpablo luengo23:07 13 Dec '06  
QuestionRadio Button in GridView Pinmemberdotnetguru20026:27 2 Nov '06  
GeneralSooooooo basic PinmemberBritney S. Morales10:35 31 Oct '06  
GeneralJust needed one more thing Pinmemberjoshnorman323:52 24 Oct '06  
GeneralThis is a great solution Pinmemberlmcguffee9:49 22 Jul '06  
GeneralThanks Pinmembersantosh poojari22:01 17 Jul '06  
GeneralCooool PinmemberFayez Mutairi8:52 31 May '06  
GeneralDoesn't seem to work in real world scenario: Pinmemberdirc23:09 11 Apr '06  
GeneralThis is the most basic thing PinmemberAlomgir Miah9:27 13 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 13 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by azamsharp
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project