CheckAll in GridView

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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
 
Mike,
this is probably more of an ASP.NET group question than a C# language group
one.
I believe your issue is here:
CheckBox chkBox =
(CheckBox)Results.FindControl("RowLevelCheckBox");

you are saying Results.FindControl - results is entire resultset, right?
Don't you want to do this at the row level of your foreach? , e.g. something
like
gvr.FindControl, or gvr["whatever"]

Peter
 
Peter,

Results is the name of the gridview, so I think my code should work.
Since it doesn't I'm obviously missing something, or doing something
wrong.
 
That was my point. you're iterating over the rows in the gridview, so you
want to use the FindControl method of the GridViewRow object, not the main
gridview.
Also, there are different types of rows - a DataRow being one that's not a
header and which holds displayed results data.
Peter
 
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>
&nbsp;<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;
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
 
Back
Top