mercredi 17 février 2016

Get checkbox checked from gridview in asp

this is code i used to add checkbox for gridview

    foreach (GridViewRow row in GridViewPhanQuyen.Rows)
            {

                for (int i = 1; i < row.Cells.Count; i++)
                {
                    string id = row.Cells[0].Text + "+" + GridViewPhanQuyen.Columns[i].HeaderText;
                    CheckBox cBox = new CheckBox();
                    cBox.ID = id;
                    row.Cells[i].Controls.Add(cBox);
                }
            }

this is code i used get checkbox checked from gridview

    foreach (GridViewRow row in GridViewPhanQuyen.Rows)
        {
            for (int i = 1; i < row.Cells.Count; i++)
            {
                string id = row.Cells[0].Text + "+" + GridViewPhanQuyen.Columns[i].HeaderText;
                bool a = ((CheckBox)row.Cells[i].FindControl(id)).Checked;

                if (a) { showMessageOk("lạ"); }



            }

when i run it, this error appears

    Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 168: { Line 169: string id = row.Cells[0].Text + "+" + GridViewPhanQuyen.Columns[i].HeaderText; Line 170: bool a = ((CheckBox)row.Cells[i].FindControl(id)).Checked; Line 171: Line 172: if (a) { showMessageOk("lạ"); }

Please, help me fix it :<>: thanks.




Aucun commentaire:

Enregistrer un commentaire