I checked your code it’s just used to Select All or Reject all once at a time not any mutual selection or deselection for other Column checkboxes in your script.
also assigning checkbox value is based on your gridId + '_ctl' + i + objectId but some time it will be diffrent if you check it in browsers so you need to check it. check the below image how the id value is genrated. Based on that you need to find the childcheckbox and then you have to apply checked value.
Check the below sample example for your reference how to work with two checkboxes in header for SelectAll and DeselectAlll functionality for two diffrent header checkboxes depending on selection or deselection.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function dgSelectAllorUnselectAll(grdid, obj, objlist, UncheckcheckboxId, uncheckboxChildId) {
//this function decides whether to check or uncheck all
if (obj.checked)
DGSelectAll(grdid, objlist, UncheckcheckboxId, uncheckboxChildId)
else
DGUnselectAll(grdid, objlist)
}
function DGSelectAll(grdid, objid, UncheckboxparentId, UncheckboxchildId) {
//this function is to check all the items
var uncheck = document.getElementById(UncheckboxparentId);
if (uncheck != null) {
uncheck.checked = false;
var i = 0;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + UncheckboxchildId + '_' + i);
while (chkbox != null) {
chkbox.checked = false;
i = i + 1;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + UncheckboxchildId + '_' + i);
}
}
var chkbox;
var i = 0;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + objid + '_' + i);
while (chkbox != null) {
chkbox.checked = true;
i = i + 1;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + objid + '_' + i);
}
}
function DGUnselectAll(grdid, objid) {
//this function is to uncheckcheck all the items
var chkbox;
var i = 0;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + objid + '_' + i);
while (chkbox != null) {
chkbox.checked = false;
i = i + 1;
//chkbox = document.getElementById(grdid + '__ctl' + i + '_' + objid);
chkbox = document.getElementById(grdid + '_' + objid + '_' + i);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="dg_Business_Tour" runat="server" AutoGenerateColumns="False">
<SelectedItemStyle Wrap="False"></SelectedItemStyle>
<ItemStyle Wrap="False"></ItemStyle>
<HeaderStyle Font-Size="7pt" Font-Names="Verdana" Font-Bold="True" Wrap="False" CssClass="CellTop">
</HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Select" HeaderStyle-Width="7%">
<HeaderTemplate>
<input id="chkAll" type="checkbox" onclick="dgSelectAllorUnselectAll('dg_Business_Tour',this,'chkClicked','chkRejAll','chkReject')">Select
All
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkClicked" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Reject" HeaderStyle-Width="6%">
<HeaderTemplate>
<input id="chkRejAll" type="checkbox" onclick="dgSelectAllorUnselectAll('dg_Business_Tour',this,'chkReject','chkAll','chkClicked')" />Reject
all
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkReject" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Id",typeof(string))
});
dt.Rows.Add("1");
dt.Rows.Add("2");
dt.Rows.Add("3");
dt.Rows.Add("4");
dt.Rows.Add("5");
dg_Business_Tour.DataSource = dt;
dg_Business_Tour.DataBind();
}
}
Output