4

I'm using a checkbox column in a gridview which is populated from a SQL database. A button below the gridview should retrieve the data of the rows whose checkboxe's have been checked. When I loop over all the rows checkboxe's, none of them has checked==true even though I have checked them all before clicking the button. Here is the ASP.NET code:

<asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkRow" runat="server" />
        </ItemTemplate>
</asp:TemplateField>

and here is the button's action that loops over all the rows' checkboxes:

protected void GetSelectedRecords(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);               
            if (chkRow != null && chkRow.Checked) //chkRow.Checked is always "false" 
            {
                string name = row.Cells[2].Text;
            }
        }  
     }              
}

Thanks a lot in advance. I'd greatly appreciate any help.

4
  • Try this:- row.FindControl("chkRow") as CheckBox Commented Dec 14, 2014 at 12:30
  • Thanks. I've already tried this one but didn't work either. Commented Dec 14, 2014 at 12:31
  • Can you post the complete code I mean including Mark-up? Something else must be happening cz your code looks fine. Commented Dec 14, 2014 at 14:01
  • Thanks for your concern; the problem has been solved. Commented Dec 15, 2014 at 13:14

1 Answer 1

7

If you are binding your grid in Page_Load, Make sure you are not binding your grid outside of if(!IsPostBack){}. Otherwise you will loose the checkboxe's on each postback and therefore losing the status of checkboxe's.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
       //Bind Your Grid Here
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This way exactly the problem. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.