How to retain the checkbox checked state in Gridview PageIndexChanging using ASP.Net? List<int> arrSelectedItems; protected void Page_Load(object sender, EventArgs e) { arrSelectedItems = new List<int>(); } protected void gvProductName_PageIndexChanging(object sender, GridViewPageEventArgs e) { try { SaveCheckBoxState(); gvProductName.PageIndex = e.NewPageIndex; if (ViewState["BindData"] != null) { DataTable dtProduct = (DataTable)ViewState["BindData"]; gvProductName.DataSource = dtProduct; gvProductName.DataBind(); } } catch (Exception ex) { } } private void SaveCheckBoxState() { if (arrSelectedItems != null) { if (ViewState["SaveState"] != null) { arrSelectedItems = (List<int>)ViewState["SaveState"]; } foreach (GridViewRow item in gvProductName.Rows) { try { if (item.RowType == DataControlRowType.DataRow) { CheckBox chk = (CheckBox)(item.Cells[0].FindControl("chkSelect")); if (chk.Checked) { if (!arrSelectedItems.Contains(Convert.ToInt32(gvProductName.DataKeys[item.RowIndex].Values["Product_Id"].ToString()))) arrSelectedItems.Add(Convert.ToInt32(gvProductName.DataKeys[item.RowIndex].Values["Product_Id"].ToString())); } else { if (arrSelectedItems.Contains(Convert.ToInt32(gvProductName.DataKeys[item.RowIndex].Values["Product_Id"].ToString()))) arrSelectedItems.Remove(Convert.ToInt32(gvProductName.DataKeys[item.RowIndex].Values["Product_Id"].ToString())); } } ViewState["SaveState"] = arrSelectedItems; } catch (Exception ex) { } } } } protected void gvProductName_PreRender(object sender, EventArgs e) { try { if (arrSelectedItems != null) { arrSelectedItems = (List<int>)ViewState["SaveState"]; foreach (GridViewRow item in gvProductName.Rows) { if (item.RowType == DataControlRowType.DataRow) { CheckBox chkbox = (CheckBox)item.FindControl("chkSelect"); if (arrSelectedItems != null) { if (arrSelectedItems.Contains(Convert.ToInt32(gvProductName.DataKeys[item.RowIndex].Values["Product_Id"].ToString()))) { chkbox.Checked = true; } } } } } } catch (Exception ex) { } }
Tuesday, December 3, 2013
How to retain the checkbox checked state in Gridview PageIndexChanging using ASP.Net?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment