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

Introduction:

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

Big deal about Radio Buttons:

You must be wondering that what is the big deal about it since I can simply place a web server RadioButton control inside the 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 that so what I will give the name to the RadioButtons and that will 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 the GridView control then the GridView control will assign them the name at runtime which will be different.


You should also check out "Select Multiple CheckBoxes Inside the GridView Control".

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

Using the HTML RadioButton:

The solution is pretty simple. Just use HTML RadioButton instead of the RadioButton web server control. 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 RadioButton.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"

BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Georgia">

<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 TemplateColumn bold so it will be easier for you to identify. I have given the name "MyRadioButton" to the HTML RadioButton. Since, I want the primary key of the row when RadioButton is checked that is why I have bound it to the value of the CategoryID.


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

As you can also see that there is a Button control of the form. Now I want that when I press the button I will get the CategoryID of the row whose RadioButton 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 like in this case "MyRadioButton" and returns the value back to the variable.

I have attached the source code files so please feel free to download it.

I hope you liked this article, happy coding!

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW