How to make single radio button selection in gridview using javascript In Asp.net
The Following Is html for your aspx page
In above Page you can see javascript function CheckOtherIsCheckedByGVID that is call on radio button on click that allow only
single radio button selection in gridview
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function CheckOtherIsCheckedByGVID(spanChk) {
var IsChecked = spanChk.checked;
if (IsChecked) {
}
var CurrentRdbID = spanChk.id;
var Chk = spanChk;
Parent = document.getElementById("<%=GridView1.ClientID%>");
var items = Parent.getElementsByTagName('input');
for (i = 0; i < items.length; i++) {
if (items[i].id != CurrentRdbID && items[i].type == "radio") {
if (items[i].checked) {
items[i].checked = false;
items[i].parentElement.parentElement.style.backgroundColor = 'white'
items[i].parentElement.parentElement.style.color = 'black';
}
}
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Primary" ItemStyle-Width="50px">
<ItemTemplate>
<asp:RadioButton ID="rbowner" runat="server" Checked='<%#Eval("Contact_Primary").ToString().Equals("True")%>' onclick="javascript:CheckOtherIsCheckedByGVID(this);" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Primary" ItemStyle-Width="50px">
<ItemTemplate>
<%#Eval("contactname")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Primary" ItemStyle-Width="50px">
<ItemTemplate>
<%#Eval("email")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
The Following is Code Behind For your Default.Vb Page
The Below code fill gridview on page Load
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("contactname", GetType(String)), _
New DataColumn("email", GetType(String)), _
New DataColumn("Contact_Primary", GetType(Boolean
))})
dt.Rows.Add("Shahid", "[email protected]", True)
dt.Rows.Add("Karib", "[email protected]", False)
dt.Rows.Add("Vineet", "[email protected]", False)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
End Class