How To Add Mutually Exclusive CheckBox inside GridView

This blog post will demonstrate the process of integrating mutually exclusive checkboxes within an ASP.NET GridView. Additionally, I will provide further details and insights to enhance your understanding of this topic.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
   <title>Untitled Page</title>
   <script type="text/javascript" language="javascript">
   function uncheckOthers(id)
       var elm = document.getElementsByTagName('input');      
       for(var i = 0; i < elm.length; i++)
           if(elm.item(i).id.substring('_')) =='_')))
               if( elm.item(i).type == "checkbox" && elm.item(i)!=id)
                   elm.item(i).checked = false;
   <form id="form1" runat="server">
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
               CellPadding="4" ForeColor="#333333" GridLines="None">
                   <asp:TemplateField HeaderText="Select">
                           <asp:CheckBox ID="CheckBox1" runat="server" />
                   <asp:BoundField DataField="Contact Name" HeaderText="Name" />
               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
               <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
               <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
               <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <EditRowStyle BackColor="#999999" />
               <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MutuallyexGrid : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            GridView1.DataSource = _sampleData;

    public DataTable _sampleData
            DataTable dt = (DataTable)Session["DataTable"];
            if (dt == null)
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("Contact Name", typeof(string)));
                dt.Columns.Add(new DataColumn("Company Name", typeof(string)));
                dt.Columns.Add(new DataColumn("City", typeof(string)));
                dt.Columns.Add(new DataColumn("Country", typeof(string)));

                dt.Rows.Add(new object[] { "Maria Anders", "Alfreds Futterkiste", "Berlin", "Germany" });
                dt.Rows.Add(new object[] { "Ana Trujillo", "Emparedados y helados ", "México D.F.", "Mexico" });
                dt.Rows.Add(new object[] { "Antonio Moreno", "Antonio Moreno Taquería", "México D.F.", "Mexico" });
                Session["DataTable"] = dt;
            return dt;
            Session["DataTable"] = value;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                string strScript = "uncheckOthers(" + ((CheckBox)e.Row.Cells[0].FindControl("CheckBox1")).ClientID + ");";
                ((CheckBox)e.Row.Cells[0].FindControl("CheckBox1")).Attributes.Add("onclick", strScript);
        catch (Exception Ex)
            //report error

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads