check checkbox inside gridview either it is checked or not?

c#

Solution

Use logical and operator `&&` instead of bitwise `&` operator to combine the condition in the if statement.

Change

if (chk != null & chk.Checked)

To

if (chk != null && chk.Checked)

Edit based on comments by OP

You need to check that you bind the grid in such a way that it is not binded on postback.

if(!Page.IsPostBack)
{
       //bind here
} 

Problem

I have gridview which contains the checkbox as templatefield. I have tried so hard but I am still unable to get the desired results, that is if check box is checked, than perform action-1 otherwise perform action-2, but everytime it is performing action-2. Below is my code I need little help from your side. Gridview code: ``` <asp:GridView ID="final" runat="server" AutoGenerateColumns="False";> <Columns> <asp:BoundField DataField="name" HeaderText="Employee Name" SortExpression="date" /> <asp:BoundField DataField="ldate" HeaderText="Date Of Leave" SortExpression="ldate" /> <asp:TemplateField HeaderText="Half/Full"> <ItemTemplate> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Enabled="true" Value="Half">Half</asp:ListItem> <asp:ListItem Enabled="true" Value="Full">Full</asp:ListItem> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Approve"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> ``` code I have made to check the radio and checkbox: ``` DataTable dtable = new DataTable(); dtable.Columns.Add(new DataColumn("Date", typeof(DateTime))); dtable.Columns.Add(new DataColumn("Half/Full", typeof(float))); dtable.Columns.Add(new DataColumn("Status", typeof(string))); Session["dt"] = dtable; SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString; conn.Open(); foreach (GridViewRow gvrow in final.Rows) { dtable=(DataTable)Session["dt"]; CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1"); if (chk != null & chk.Checked) { RadioButtonList rButton = (RadioButtonList)gvrow.FindControl("RadioButtonList1"); if (rButton.SelectedValue == "Half") { //perform action-1 } else { //perform action-1 } } else { perform action-2 } } ``` everytime it's going into the last else...why?

Original source