On May 16, 8:16 pm, Mike P <mike.p...@gmail.comwrote:
I am trying to add check all functionality to my grid view, but I can't
get it to work. Here is my gridview :
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server"
ID="RowLevelCheckBox" />
</ItemTemplate>
</asp:TemplateField>
And here is the code :
protected void CheckAll_Click(object sender, EventArgs e)
{
foreach(GridViewRow gvr in Results.Rows)
//for (int i = 0; i < Results.Rows.Count; i++)
{
CheckBox chkBox =
(CheckBox)Results.FindControl("RowLevelCheckBox");
chkBox.Checked = true;
}
}
But I am getting the error 'object ref not set to instance of object' on
the line chkBox.Checked = true.
Can anybody help me out with this?
Thanks,
Mike
*** Sent via Developersdexhttp://www.developersdex.com***
Hi Mike,
You can do this "CheckAll" functionality using java script. Here is
the piece of code which does this.
GridView
--------------
<asp:TemplateField>
<HeaderTemplate>
<input
type="checkbox" id="chk_selectAll" runat="server"
onclick="checkAll(this);" />
</HeaderTemplate>
<HeaderStyle
HorizontalAlign="Center" />
<ItemTemplate>
<input type="checkbox"
runat="server" id="chk_select" value='<%# Eval("TimesheetId") %>'/>
</ItemTemplate>
<ItemStyle
HorizontalAlign="Center" />
</asp:TemplateField>
Js
---
function checkAll(chk_SelectAll)
{
var frm = document.forms[0];
var chkState = chk_SelectAll.checked;
for(i = 0 ; i < frm.length; i++)
{
var el = frm.elements[i];
if(el.type == "checkbox" && el.name.indexOf('chk_select') !
= -1 )
{
el.checked = chkState;
}
}
}
I am using this code and it works. Hope this will help you to fix
the problem.
Regards,
Ram