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<=====================================================================

Dynamic Gridview binding in asp.net c# by getting data from StoredProcedure


Table Design:

TableDesign Data In Table:

DatainTable

Stored Procedure:

create procedure sp_GetCustomers
as
begin
select * from dbo.customer
end

Code in Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code in Default.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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=SourceName;Initial Catalog=DbName;User ID=sa;Password=YourPassword");
        SqlCommand cmd = new SqlCommand("sp_GetCustomers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

OutPut:

Dynamic Gridview binding in asp.net c# by getting data from StoredProcedure

How to append text to a Eval databound field in gridview in asp.net?


We will use Databinders to bind data to controls.

Sometimes we need to append some text before data or after data… ex: we can append Download text before to DocumentName

we can do this as follows

Append Text before Eval Data

  1. <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#"Download" +" "+ Eval("DocumentName").ToString() %>'/>

Append Text before and after Eval Data

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#"Download" +" "+ Eval("DocumentName").ToString()+" "+ "TextFile"%>'/>

Concatenating Two Eval binders

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#"Download" +" "+ Eval("DocumentName").ToString()+" as "+ Eval("Format").ToString()%>'/>

By using string.Format() Method in Eval

Append Text before Eval Data

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#string.Format("Download {0}",Eval("DocumentName")) %>'/>

Append Text before and after Eval Data

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#string.Format("Download {0} as TextFile",Eval("DocumentName")) %>'/>

Concatenating Two Eval binders

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/DwonloadFile.png" ToolTip='<%#string.Format("Download {0} as {1}",Eval("DocumentName"),Eval("Format")) %>'/>

Thanks…

Venki

Hi if this is not solving ur problem please leave ur requirement as a comment below this post…