Select single RadioButton in GridView when RadioButton’s GroupName is not working in asp.net using JavaScript


Here we will learn how to avoid Radiobutton or RadioButtonList allows multiple selections in GridView without using any RadioButton’s Group in asp.net C#
First Create Table as follows

tbl_Employee

Add the data into the table

Data

JavaScript Code:

<script language=”javascript” type=”text/javascript”>
function SetUniqueRadioButton(index) {
for (i = 0; i < IsManager.length; i++) {
var rb_Visitor = document.getElementById(IsManager[i]);
if (i == index) {
rb_Visitor.checked = true;
}
else {
rb_Visitor.checked = false;
}
}
}
</script>

Form Content:

<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”
OnRowDataBound=”GridView1_RowDataBound” onprerender=”GridView1_PreRender”>
<Columns>
<asp:BoundField DataField=”EmpId” HeaderText=”Emp Id” />
<asp:BoundField DataField=”EmpName” HeaderText=”Emp Name” />
<asp:TemplateField HeaderText=”Is Manager”>
<ItemTemplate>
<asp:RadioButton ID=”rb_IsManager” GroupName=”ss” runat=”server” Text=’<%#Eval(“IsManager”) %> />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”DOJ” HeaderText=”Date of Join”
DataFormatString=”{0:d}” />
</Columns>
</asp:GridView>
</div>
</form>

ConnectionString in Web.Config file:

<connectionStrings>
        <addname=ConnectionStringconnectionString=Data Source=YourServerName;Initial Catalog=DemoDB;Persist Security Info=True;User ID=sa;Password=yourPassword/>
    </connectionStrings>

NameSpaces To Use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

Filling GridView:

protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
string connectionstring = ConfigurationManager.ConnectionStrings[“ConnectionString”].ToString();
public void GetData()
{
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(“select EmpId,EmpName,IsManager,DOJ from tbl_employee”, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

Binding OnClick Javascript Method with RowIndex to RadioButton in GridView RowDataBound Event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = (GridViewRow)e.Row;
RadioButton rb_IsManager = (RadioButton)row.FindControl(“rb_IsManager”);
rb_IsManager.Attributes.Add(“onclick”, “SetUniqueRadioButton(“ + row .DataItemIndex + “)”);
if (Convert.ToBoolean(rb_IsManager.Text))
{
rb_IsManager.Text = “”;
rb_IsManager.Checked = true;
}
else
{
rb_IsManager.Text = “”;
rb_IsManager.Checked = false;
}
}
}

Collecting all RadioButtons ClientIds in GridView PreRender Event:

protected void GridView1_PreRender(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
foreach (GridViewRow grdrow in GridView1.Rows)
{
RadioButton rb_IsManager = (RadioButton)grdrow.FindControl(“rb_IsManager”);
cs.RegisterArrayDeclaration(“IsManager”, String.Concat(“‘”, rb_IsManager.ClientID, “‘”));
}
}

Complete Code:

(.aspx)

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Radiobuttongroup in grid.aspx.cs”
Inherits=”Radiobuttongroup_in_grid” %><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>

<script language=”javascript” type=”text/javascript”>
function SetUniqueRadioButton(index) {
for (i = 0; i < IsManager.length; i++) {
var rb_Visitor = document.getElementById(IsManager[i]);
if (i == index) {
rb_Visitor.checked = true;
}
else {
rb_Visitor.checked = false;
}
}
}
</script>

</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”
OnRowDataBound=”GridView1_RowDataBound” onprerender=”GridView1_PreRender”>
<Columns>
<asp:BoundField DataField=”EmpId” HeaderText=”Emp Id” />
<asp:BoundField DataField=”EmpName” HeaderText=”Emp Name” />
<asp:TemplateField HeaderText=”Is Manager”>
<ItemTemplate>
<asp:RadioButton ID=”rb_IsManager” GroupName=”ss” runat=”server” Text=’<%#Eval(“IsManager”) %> />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”DOJ” HeaderText=”Date of Join”
DataFormatString=”{0:d}” />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

(.aspx.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;public partial class Radiobuttongroup_in_grid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
string connectionstring = ConfigurationManager.ConnectionStrings[“ConnectionString”].ToString();
public void GetData()
{
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(“select EmpId,EmpName,IsManager,DOJ from tbl_employee”, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = (GridViewRow)e.Row;
RadioButton rb_IsManager = (RadioButton)row.FindControl(“rb_IsManager”);
rb_IsManager.Attributes.Add(“onclick”, “SetUniqueRadioButton(“ + row .DataItemIndex + “)”);
if (Convert.ToBoolean(rb_IsManager.Text))
{
rb_IsManager.Text = “”;
rb_IsManager.Checked = true;
}
else
{
rb_IsManager.Text = “”;
rb_IsManager.Checked = false;
}
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
foreach (GridViewRow grdrow in GridView1.Rows)
{
RadioButton rb_IsManager = (RadioButton)grdrow.FindControl(“rb_IsManager”);
cs.RegisterArrayDeclaration(“IsManager”, String.Concat(“‘”, rb_IsManager.ClientID, “‘”));
}
}
}

OutPutScreen:

Select single RadioButton in GridView when RadioButton's GroupName is not working in asp.net using JavaScript

=======================================================>0<=====================================================================

Happy Coding

=======================================================>0<=====================================================================

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s