2

I am using checkbox in the gridview header to select all records in the gridview. this is the code behind method.

<asp:GridView ID="gvP" runat="server" AutoGenerateColumns="false" AllowPaging="false"
AllowSorting="false" DataKeyNames="PmtId" CssClass="list-table" HeaderStyle-CssClass="header"
EnableModelValidation="True">
    <Columns>
    <asp:BoundField DataField="PmtId" HeaderText="PmtId" ReadOnly="True" Visible="false" />
    <asp:TemplateField HeaderText="All" ItemStyle-CssClass="checkbox-col" HeaderStyle-CssClass="checkbox-col">
    <HeaderStyle HorizontalAlign="Center" />
    <HeaderTemplate>
    <input id="chkBoxAll" type="checkbox" onclick='javascript:checkAllBoxes("CLEAR_PT")' />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox runat="server" ID="chkSelect" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="S/No." ItemStyle-CssClass="seq-col" HeaderStyle-CssClass="seq-col">
    <ItemTemplate>
    <%# Eval("SNo")%>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView> 

protected void chkSelectAll(string arg)
{
    if (arg.Equals("CLEAR_PT"))
    {
    CheckBox chkAll = gvP.HeaderRow.FindControl("chkBoxAll") as CheckBox;
    if (chkAll.Checked == true)
    {
    foreach (GridViewRow gvRow in gvP.Rows)
    {
    CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect") as CheckBox;
    chkSel.Checked = true;
    }
    }
    else
    {
    foreach (GridViewRow gvRow in gvP.Rows)
    {
    CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect") as CheckBox;
    chkSel.Checked = false;
    }
    }
    }
}

I am getting 'Object reference not set to an instance of an object' run time error in the following line.

if (chkAll.Checked == true)

Any Idea?

2
  • Could you provide your gridview aspx code? Commented Feb 27, 2012 at 2:40
  • Updated above. please check now. Commented Feb 27, 2012 at 2:45

1 Answer 1

1

Your checkbox control in header template is html input control. You need to change as ASP.NET check box control. Change as server checkbox control and try. Actually check all and uncheck all can be done in client side javascript. No need postback.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.