In my gridview, I inserted a column that contains a checkbox in the header:
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:CheckBox ID="chkBxHeader" Text="<br />Select"
AutoPostBack="true"
OnCheckedChanged="chkBxHeader1_CheckedChanged"
runat="server" />
</HeaderTemplate>
<ItemTemplate>
<div>
<br />
<asp:CheckBox ID="chkRow" runat="server" />
<asp:ImageButton ID="btnchkRow"
ImageUrl="/img/checkbox_closed.gif"
runat="server" />
</div>
</ItemTemplate>
</asp:TemplateField>
Selecting this checkbox will perform the following procedure chkBxHeader1_CheckedChanged which follows the validation of the gridview rows:
try
{
CheckBox chkBxHeader = (CheckBox)sender;
bool isvalid = false;
foreach (GridViewRow row in gvProducts.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (!(row.FindControl("chkRow") as CheckBox).Checked)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert",
"alert('KO');", true);
chkBxHeader.Checked = false;
isvalid = false;
break;
}
else
{
chkBxHeader.Checked = true;
chkBxHeader.Enabled = false;
isvalid = true;
}
}
}
// Process only if valid.
if (isvalid)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert",
"alert('OK');", true);
}
}
catch (Exception ex)
{
Console.WriteLine("Skipped exception! " + ex);
}
If the procedure is successful I disable the checkbox in the column header.
My problem is when I return to the gridview when I have closed the browser or changed the web page even though all the checkboxes are selected the checkbox in the column header is always visible and selectable.
In this case the checkbox in the column header must be hidden or inhibited.
I have tried this in GridView1_RowDataBound, without success:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
int counter = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
counter++;
}
}
if (counter == 9)
{
CheckBox chkBxHeader = (CheckBox)e.Row.FindControl("chkBxHeader");
Response.Write(counter + "<br />");
chkBxHeader.Visible = false;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Skipped exception! " + ex);
}
}
Update 2025/03/25
if (ds.Tables[0].Rows.Count > 0)
{
dt = ds.Tables[0];
CheckBox chkHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkBxHeader");
// if 9 or more rows are checked, then disable heading check box
int ckCount = 0;
foreach (GridViewRow gRow in GridView1.Rows)
{
CheckBox chkRow = (CheckBox)gRow.FindControl("chkRow");
if (chkRow.Checked)
ckCount++;
}
if (ckCount >= 9)
{
chkHeader.Enabled = false;
}
else
{
chkHeader.Enabled = true;
}
}
Error
Server Error in '/aspnet' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 1102: dt = ds.Tables[0];
Line 1103:
Line 1104: CheckBox chkHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkBxHeader");
Line 1105: // if 9 or more rows are checked, then disable heading check box
Line 1106:
Update 2025/03/27 Solved
int counter = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (e.Row.FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
counter++;
}
}
if (counter == 9)
{
CheckBox chkBxHeader = GridView1.HeaderRow.FindControl("chkBxHeader") as CheckBox;
chkBxHeader.Checked = true;
chkBxHeader.Enabled = false;
}
}
if (counter == 9)so, only if the results count is 9 will it hide it?