I need check whether CheckBox in GridView is checked or not in c# and ASP.Net and to check that all the checkboxes in my gridview are selected, some are selected and some are not, according to a predefined condition, the previous column in the gridview must equal OK.
If all the checkboxes of the gridview are selected I have to continue to execution of the page, otherwise if also even if only one checkbox is not selected I have to block the execution of the page..
I don’t have error but the check whether CheckBox in GridView is checked not working.
My code below.
.aspx
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:CheckBox ID="chkBxHeader"
AutoPostBack="true" OnCheckedChanged="chkBxHeader_CheckedChanged" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<div>
<asp:CheckBox ID="chkRow" runat="server" />
</div>
</ItemTemplate>
</asp:TemplateField>
.cs
protected void chkBxHeader_CheckedChanged(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('KO');", true);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Skipped exception! " + ex);
}
}