473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

CheckAll in GridView

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 Developersdex http://www.developersdex.com ***
May 16 '07 #1
4 3969
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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Mike P" wrote:
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 Developersdex http://www.developersdex.com ***
May 16 '07 #2
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.

*** Sent via Developersdex http://www.developersdex.com ***
May 17 '07 #3

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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Mike P" wrote:
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.

*** Sent via Developersdex http://www.developersdex.com ***
May 17 '07 #4
Ram
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>
&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[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

May 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
4
by: slugstone | last post by:
I am trying to get a checkall/uncheckall working for my form with javascript on an apache web server. I know I am doing something wrong and can not get the proper syntax for the javascript to work....
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
6
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.