Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have grid view, in which I have a checkbox column. The problem is that when I click on the header row checkbox, it marks all row checkboxes, but when I do paging and come back to same page, the header check box state is not maintaining but other are in marked state.

So I want to maintain my top checkbox which selects all checkboxes

Here is my code:

C#
protected void grdEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grdEmployee.PageIndex = e.NewPageIndex;
 
                List<string> listEmpId = new List<string>();
                if (hdnAdvanceSearch.Value == "Normal")
                {
                    if (ViewState["empListNormal"] != null)
                    {
                        listEmpId = (List<string>)ViewState["empListNormal"];
                    }
                    foreach (GridViewRow row in grdEmployee.Rows)
                    {
                        CheckBox chkRow = (CheckBox)row.FindControl("chkRow");
                        CheckBox chkHeader = (CheckBox)row.FindControl("chkHeader");
                        HiddenField hdnEmployeeID = (HiddenField)row.FindControl("hdnEmployeeID");
                        
                        if (chkRow.Checked)
                        {
                            if (!listEmpId.Contains(hdnEmployeeID.Value))
                            {
                                listEmpId.Add(hdnEmployeeID.Value);
                            }
                        }
                        else
                        {
                            if (listEmpId.Contains(hdnEmployeeID.Value))
                            {
                                listEmpId.Remove(hdnEmployeeID.Value);
                            }
                        }
                    }
                    ViewState["empListNormal"] = listEmpId;
                    BindEmployeeGrid();
                }
                else if (hdnAdvanceSearch.Value == "Advanced")
                {
                    if (ViewState["empListAdvanced"] != null)
                    {
                        listEmpId = (List<string>)ViewState["empListAdvanced"];
                    }
                    foreach (GridViewRow row in grdEmployee.Rows)
                    {
                        CheckBox chkRow = (CheckBox)row.FindControl("chkRow");
                        HiddenField hdnEmployeeID = (HiddenField)row.FindControl("hdnEmployeeID");
                        if (chkRow.Checked)
                        {
                            if (!listEmpId.Contains(hdnEmployeeID.Value))
                            {
                                listEmpId.Add(hdnEmployeeID.Value);
                            }
                        }
                        else
                        {
                            if (listEmpId.Contains(hdnEmployeeID.Value))
                            {
                                listEmpId.Remove(hdnEmployeeID.Value);
                            }
                        }
                    }
                    ViewState["empListAdvanced"] = listEmpId;
                    BindAdvancedEmployeeGrid();
                }
            }
            catch (Exception ex)
            { 
               
            }
        }
Posted
Updated 15-Jul-13 23:06pm
v3
Comments
Torakami 16-Jul-13 5:06am    
what changes have you made can you shoe me that

 
Share this answer
 
Comments
Torakami 17-Jul-13 2:10am    
Can Any one help me on this issue
Thanks7872 17-Jul-13 6:17am    
Good one. ↑voted.
 
Share this answer
 
Comments
Torakami 16-Jul-13 6:27am    
I tried this javascript .. but it also selects checkbox outside the gridview as well .. I dont know why
Dholakiya Ankit 16-Jul-13 6:28am    
give me code which you have tried
Torakami 16-Jul-13 6:37am    
function SelectAllCheckboxes(spanChk) {
debugger;
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox = (spanChk.type == "checkbox") ?
spanChk : spanChk.children.item[0];
xState = theBox.checked;
elm = theBox.form.elements;

for (i = 0; i < elm.length; i++)
if (elm[i].type == "checkbox" &&
elm[i].id != theBox.id) {
//elm[i].click();
if (elm[i].checked != xState)
elm[i].click();
//elm[i].checked=xState;
}
}
Torakami 16-Jul-13 6:38am    
<HeaderTemplate>
<input id="chkAll" önclick="javascript: SelectAllCheckboxes(this);"
runat="server" type="checkbox" />
<%--<asp:CheckBox ID="chkHeader" runat="server" onclick="javascript:SelectAllCheckboxes(this);"/>--%>
</HeaderTemplate>
Torakami 16-Jul-13 6:39am    
the problmem is check box gets selected outside the gridview as well and on changing the page pagination gets diabled

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900