473,388 Members | 864 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,388 software developers and data experts.

How to implement a "check all" checkbox in a datagrid

Hi all..

I have a DataGrid with checkboxes. In the header I have a "check all"
checkbox.

I'm wondering if there is an easy way to check all checkboxes using that
checkbox. I could do it using JavaScript code, but the main problem I have
is that checkboxes ids aren't kept when the datagrid is rendered, for
example, if the checkboxes have the name chkNumid, when the datagrid is
rendered, the checkboxes become dgDataGrid_ctl02_chkNumId,
dgDataGrid_ctl03_chkNumId, and so on.

Of course, this must be done at client side, without posting the page back.

Thanks in advance
Jaime
Nov 19 '05 #1
7 2880
Yeah, it's certainly doable, and you've already identified the one
thing that makes it a bit tricky. Fortunately, those ids will remain
consistent between page loads. So once you teach your script to deal
with them, you won't have to worry about it breaking as you add
controls to your page.

I'd do something like

var base = 'dgDataGrid_ctl';
var boxID = '_chkNumId';
var index = 0;

while (document.getElementById(base + index + boxID))
{
document.getElementById(base + index + boxID).checked = true;
}
Clean, simple, and effective, if a bit hacky. But that's javascript
for you.

Good Luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #2
To deal with those IDs you can create client side array in c# which will
contain .ClientID of those checkboxes.
I think it's safer.

"Jason Kester" <ja*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Yeah, it's certainly doable, and you've already identified the one
thing that makes it a bit tricky. Fortunately, those ids will remain
consistent between page loads. So once you teach your script to deal
with them, you won't have to worry about it breaking as you add
controls to your page.

I'd do something like

var base = 'dgDataGrid_ctl';
var boxID = '_chkNumId';
var index = 0;

while (document.getElementById(base + index + boxID))
{
document.getElementById(base + index + boxID).checked = true;
}
Clean, simple, and effective, if a bit hacky. But that's javascript
for you.

Good Luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #3
Do you mean to create the script in code-behind and register the script block
containing the function?

I realized that first index of the checkboxes in the data grid is 2 so I
need it first in order to program the while suggested before.

Jaime

"Sebastian Wojciechowski" wrote:
To deal with those IDs you can create client side array in c# which will
contain .ClientID of those checkboxes.
I think it's safer.

"Jason Kester" <ja*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Yeah, it's certainly doable, and you've already identified the one
thing that makes it a bit tricky. Fortunately, those ids will remain
consistent between page loads. So once you teach your script to deal
with them, you won't have to worry about it breaking as you add
controls to your page.

I'd do something like

var base = 'dgDataGrid_ctl';
var boxID = '_chkNumId';
var index = 0;

while (document.getElementById(base + index + boxID))
{
document.getElementById(base + index + boxID).checked = true;
}
Clean, simple, and effective, if a bit hacky. But that's javascript
for you.

Good Luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/


Nov 19 '05 #4
But "index" must be padded with "0" to the left and how many zeros depend on
the number of rows the datagrid contains after populated with data, although
the while solution is the best (with respect to generating an array with all
checkboxes) I will need to get what numbers of zeros to add to the left
first. Do you have another better approach?

Thanks
Jaime

"Jason Kester" wrote:
Yeah, it's certainly doable, and you've already identified the one
thing that makes it a bit tricky. Fortunately, those ids will remain
consistent between page loads. So once you teach your script to deal
with them, you won't have to worry about it breaking as you add
controls to your page.

I'd do something like

var base = 'dgDataGrid_ctl';
var boxID = '_chkNumId';
var index = 0;

while (document.getElementById(base + index + boxID))
{
document.getElementById(base + index + boxID).checked = true;
}
Clean, simple, and effective, if a bit hacky. But that's javascript
for you.

Good Luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #5
Never, ever, under any circumstances actually use RegisterScriptBlock!

<script>

var clientIDs = new Array( <asp:literal id=litClientIDs
runat=server/> );

// the rest of your javascript goes here, where you can read it, edit
it and actually make sense of it.

</script>

If you have to "pass" variables to javascript from the server, always
try to do so with the least amount of server code possible. There is
nothing uglier and less readable than a block of javascript constructed
as a string!
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #6
This js function may help you,

function fnSelectAll(){
var obj = document.getElementById("dg").all.tags("input");//ID of the
Datagrid
var blnFlag = true; //To check or to uncheck
var chk = document.getElementById("chkSelectAll"); //ID of Checkbox
at Datagrid header
if (chk.checked == true)
blnFlag=true;
else
blnFlag=false;

for (var i=0; i<obj.length; i++){
if (obj[i].type == "checkbox"){
obj[i].checked = blnFlag;
}
}
}

Expl: The first statement in the above fun. will give all the input
tags( for Checkbox Input type=checkbox) that are present in the
Datagrid. (A datagrid will be rendered in a table format). So just by
checking the validation, u can do whatever u want,

Praveen.

Nov 19 '05 #7
Thanks Praveen!! that worked!

My problem was that the first checkbox was assigned a name of ctl02 instead
of ctl01 and I don't know if it would be always that way (starting from 02),
so by using document.getElementById("dg").all.tags("input") it would work for
all cases.

Jaime

"Praveen" wrote:
This js function may help you,

function fnSelectAll(){
var obj = document.getElementById("dg").all.tags("input");//ID of the
Datagrid
var blnFlag = true; //To check or to uncheck
var chk = document.getElementById("chkSelectAll"); //ID of Checkbox
at Datagrid header
if (chk.checked == true)
blnFlag=true;
else
blnFlag=false;

for (var i=0; i<obj.length; i++){
if (obj[i].type == "checkbox"){
obj[i].checked = blnFlag;
}
}
}

Expl: The first statement in the above fun. will give all the input
tags( for Checkbox Input type=checkbox) that are present in the
Datagrid. (A datagrid will be rendered in a table format). So just by
checking the validation, u can do whatever u want,

Praveen.

Nov 19 '05 #8

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

Similar topics

4
by: Matt | last post by:
In ASP page, there is a "SELECT ALL" button, when user click it, it will select all checkboxes. I am not sure should I use client-side code to do that? the following is my approach but it didnt...
1
by: Gidrazas | last post by:
Hello I'm trying to generate DataSet from Sybase ASA 9 database. I'll use it in Crystal Reports. But when i'm trying to drag table from Server Explorer as written in...
3
by: Not Me | last post by:
Hi, Is there any criteria I can use in my where clause to say 'anything'? Maybe like the _ used in some languages? For my example, I want to use an inline-if, so if a checkbox is ticked I say...
2
by: Bernt Fischer | last post by:
Hello I want to render a datagrid with transparent borders (cellspacing = 1) so the underlying background shines through. Unfortunately, ASPNET renders the <table> tag with a 'rules="all"...
2
by: Murphy | last post by:
Our website contains subdirectories for each subsidiary company, each company has it's own look and feel to the pages in their subdirectory although they are all part of the main website. The...
1
by: | last post by:
I have an idea for a way to help maintain my websites, and I'm hoping someone can help me figure out how to implement it using C#, VB.NET, or elements of the .NET framework. I'd be putting this on...
0
by: bhavindesai | last post by:
Hello All, Can anybody pleae help me out in this? I have a datagrid control on webpart, in which I am displaying two out of six columns from a dataset. When somebody click on one row, I want to...
4
by: jojoba | last post by:
hello all, does anyone know if, for a 2-state checkbox, you can use the "fill in" marker from the 3-state checkbox, instead of a checkmark i just like the look of the "fill-in" instead of the...
4
by: chengsi | last post by:
Hi, I have a "continuous" subform which is linked to a table which has a checkbox field. I would like to create a Check All/Uncheck All checkbox control that both checks and disables the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.