Vinod
Vinod

Reputation: 4872

Checkbox state resets on postback

I want to get the "true" or "false" of all checkboxes in a gridview after a button click.
But none of the checkbox is returning "true" even if i check them.
Please write me the reason and solution.Thanks in Advance

protected void Button1_Click(object sender, EventArgs e)
        {
            foreach(GridViewRow r in GridView1.Rows)
            {
                Boolean b = ((CheckBox)GridView1.Rows[r.RowIndex].Cells[0].FindControl("cbox")).Checked;
                Response.Write(b);
            }
        }

Upvotes: 2

Views: 1818

Answers (1)

Stefan H
Stefan H

Reputation: 6683

The problem is likely that you are Rebinding the data on page load (which happens before control events).

Try wrapping your binding methods in

If (!IsPostBack)
{
    //Load  Data
}

EDIT:

Useful link is useful: http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

Upvotes: 4

Related Questions