2
votes

I have a column of checkboxes to select the records in the gridview but i am struggling to determine which checkboxes were checked on postback caused by button click.I used the following code but it doesnt work.

protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("Chkgridselect"); 
        if (cb.Checked)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
        }
    }
}

Can anyone please rectify the above coding?

1

1 Answers

2
votes

This is what i do and it is working:

foreach (GridViewRow row in GridView1.Rows)
{
    // Access the CheckBox
    CheckBox cb = (CheckBox)row.FindControl("Chkgridselect");
    if (cb != null && cb.Checked)
    {
        //dostuff
    }
}