This article will show you how you can use radiobutton control
in gridview and allow user to select only one radiobutton in asp.net using
c#.net.
Some of my previous articles are as follows: Save
Panel with Control Inside It as Image C#.net, VB.net in windows application,
Dynamically
Change Form Background Color C#.Net in Windows Application, Dynamically
Add Textbox Control in Panel and Clear All on Single Click Using C#.Net in
Windows Application, Confirmation
Message on Button Click and Capture YES/NO Click by User in Windows
application, C#.
So for this article first we will create a new asp.net
application and add the below code into the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Single
RadioButton Selection in GridView In Asp.Net Using C#.Net </title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<h3>Radiobutton
selection in GridView</h3>
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1"
runat="server"
name="userradio"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Address"
HeaderText="Address"
/>
<asp:BoundField DataField="UserType"
HeaderText="UserType"
/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above code I have used asp.net radio button. Add the code to bind the detail.
using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
private
void BindGridView()
{
SqlConnection
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource = objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
}
}
|
In above we can see that we are able to select multiple radio button. Why this is happening. This is because when we run the code at that time button name are generating dynamically and as we know that for grouping the radio button we need same name for the radio buttons.
Now check the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Single
RadioButton Selection in GridView In Asp.Net Using C#.Net </title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<h3>Radiobutton
selection in GridView</h3>
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input id="Radio1"
type="radio"
name="userrbt"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Address"
HeaderText="Address"
/>
<asp:BoundField DataField="UserType"
HeaderText="UserType"
/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above I have used html radio button and assigned the name to the radio button. Now we run the application and check the output.
Wow sir you just helped me alot! I was searching for somtheing like this and i always found old Solutions, which are not working anymore!
ReplyDeleteThank you very very much!
Is it possible to check These "Radiobuttons" now? Would like to check which one is checked and then select the row from the Gridview... is that possible?
ReplyDelete