When you bind the grid you should bind it conditionally. Condition would be if (!page.IsPostBack) then you bind grid. In your case when form post back to the server it is rebind and your checkbox selection is override. Just code example like that
<asp:gridview id="GrdATContactList" runat="server" autogeneratecolumns="false" datakeynames="Id" xmlns:asp="#unknown">
<columns>
<asp:templatefield headertext="Is Delete">
<itemtemplate>
<asp:checkbox id="chkDelete" runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:textbox id="TextBox1" runat="server" text="Hello"></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:boundfield datafield="Id" />
<asp:boundfield datafield="Name" />
</columns>
</asp:gridview>
code bebind as follows
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GrdATContactList.DataSource = GetData();
GrdATContactList.DataBind();
}
}
private IList<Data> GetData()
{
var list = new List<Data>();
list.Add(new Data {Id=1, Name="A1"});
list.Add(new Data { Id = 2, Name = "A2" });
list.Add(new Data { Id = 3, Name = "A3" });
return list;
}
protected void OnDeleteAll(object sender, EventArgs e)
{
foreach (GridViewRow row in GrdATContactList.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkDelete") as CheckBox;
if (((CheckBox)row.FindControl("chkDelete")).Checked)
{
string id = GrdATContactList.DataKeys[row.RowIndex].Value.ToString();
}
}
}
}
public class Data
{
public int Id { get; set; }
public string Name { get; set; }
}