Unable to get value of the property 'length': object is null or undefined - Javascript Error
<p>In my user control, there is a GridView.
When I check checkbox in the header of the GridView, I want to select all checkboxs.
<strong>My user control can be use more than once in a page.</strong>
So I try like this. </p>

<p>My GridView</p>

<pre><code>&lt;asp:GridView ID="GridView" runat="server"&gt;
    &lt;Columns&gt;
        &lt;asp:TemplateField&gt;
            &lt;HeaderTemplate&gt;
                &lt;asp:CheckBox ID="chkHeader" runat="server" /&gt;
            &lt;/HeaderTemplate&gt;
            &lt;ItemTemplate&gt;
                &lt;asp:CheckBox ID="chkEach" runat="server" /&gt;
            &lt;/ItemTemplate&gt;
            .
            .
</code></pre>

<p>This is my C# codes</p>

<pre><code>CheckBox chkAll = gvAttachment.Controls[0].Controls[0].FindControl("chkHeader") as CheckBox;
     if (chkAll != null)
     {
           chkAll.Attributes.Add("onclick", "SelectAllChkBox('" + chkAll.ClientID + "','" + GridView.ClientID + "');");
     }
</code></pre>

<p>This is my javascript</p>

<pre><code>function SelectAllChkBox(value , grid) {
    for (var i = 1; i &lt; grid.all.length; i++) {
        grid.all[i].checked = value.checked;
    }
}
</code></pre>

<p>But I got this error.</p>

<pre><code>Unable to get value of the property 'length': object is null or undefined
</code></pre>

<p>What is wrong with my Code ?</p>
