Home > Forum > ASP.NET > how to check if checkbox is checked?

how to check if checkbox is checked?

by salwamirza   on Jan 27, 2014   Category: ASP.NET  |  Views: 4065    |  Points: 5   |  Starter 
  Reply
Hi
I've a GV and inside an ItemTemplate I've a checkbox.

<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True">
<Columns>
<asp:TemplateField HeaderText="IsApproved">
<ItemTemplate>
<asp:CheckBox ID="chkApproved" runat="server" CommandName="Approve" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now on btnClick I want to check if the chkbox is checked or not.

protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkApproved");
if (chk != null && chk.Checked)
{
Response.Write("Checkbox is checked");
}
}
}
But this doesn't seem to do anything even if the checkbox is checked or its null.

When debugging it never steps into the If statement when I have the checkbox checked in the browser. Ideas?!

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :how to check if checkbox is checked?   
by Thamil
on Jan 28 2014 6:55AM
Points : 10
Gold 
Hi Salwamirza,

Make sure that you are binding the GridView in Not IsPostBack condition in page load event

also check with this below code

protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)row.FindControl("chkApproved");
if (chk != null && chk.Checked)
{
Response.Write("Checkbox is checked");
}
}
}
}

Recent Post

Latest Posts