PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Checking/unchecking all checkboxes in the one column of the gridview

 
 
schapopa
Guest
Posts: n/a
 
      18th May 2010
Hi,
I have gridview that contains many columns with checkboxes.
I added one more row below header that contains link buttons and I would
like to be able to check and uncheck all checkboxes in one column by
clicking on link button.
I saw some javascript solutions that do that but in case there is one
column with checkboxes. I am strugling with making that work for just
one column.
Does anyone has solution or link on how to make it work.
Thank you
Arek



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      18th May 2010
<schapopa> wrote in message news:(E-Mail Removed)...

> Does anyone has solution or link on how to make it work.


There are essentially two ways of doing this:

1) Create individual client-side JavaScript arrays of the various sets of
checkbox IDs as the page loads. Each array contains the checkbox IDs for a
single column. Then include an extra argument in the client-side JavaScript
function which checks / unchecks the checkboxes so that it knows which array
to iterate through. This is described here:
http://www.4guysfromrolla.com/articles/052406-1.aspx

2) Use "intelligent" naming for the checkboxes and link buttons so that the
client-side JavaScript can tell them apart. E.g. call the link button in
column 1 lnkCol1 and then give the <asp:CheckBox /> in the corresponding
TemplateField an ID of chkCol1. When the page is built, ASP.NET will munge
the control names to prevent duplicates, of course, but that doesn't
matter - there will be enough to uniquely identify each checkbox. Then
include an extra argument in the client-side JavaScript function, e.g.

function CheckAll(colID)
{
for(i = 0; i <
document.getElementById('<%=MyGridView.ClientID%>').length; i++)
{
objElement = document.forms[0].elements[i];
if (objElement.type == 'checkbox' && objElement.id.indexOf(colID)
> -1)

{
objElement.checked = true;
}
)

Finally, in the markup for the LinkButtons, add e.g.
OnClientClick="CheckAll('Col1');"

Obviously, the first solution is more efficient because it does not need to
walk through the entire collection of elements behind which comprise the
HTML rendered from the GridView, but it involves more work to set up
initially. The second solution is much easier and quicker to code so, unless
the GridView contains a *HUGE* amount of checkboxes, that's the one I'd
almost certainly go for...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
schapopa
Guest
Posts: n/a
 
      19th May 2010
Almost working, where almost makes a big difference
"&& objElement.id.indexOf(colID) > -1" is this some kind of search
similar to "like" in sql?

Thank you for your help, I think I am getting closer.

Arek



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      19th May 2010
<schapopa> wrote in message news:#(E-Mail Removed)...

> Almost working, where almost makes a big difference
> "&& objElement.id.indexOf(colID) > -1" is this some kind of search
> similar to "like" in sql?


No. indexOf is the JavaScript method for determining where one piece of text
occurs in another piece of text:
http://www.google.co.uk/search?aq=f&...Script+indexOf


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking and Unchecking CheckBoxes using code Ayo Microsoft Excel Programming 3 4th Apr 2009 12:06 PM
Checking and Unchecking CheckBoxes in code Ayo Microsoft Excel Misc 0 3rd Apr 2009 08:41 PM
Programmatically adding a column of Checkboxes to Gridview d.s.stevenson@gmail.com Microsoft C# .NET 3 30th Apr 2008 02:14 AM
checkboxes unchecking in VB code =?Utf-8?B?QW1hdGV1cg==?= Microsoft Access 4 15th May 2007 08:11 PM
Update a dataset used by a gridview(uses paging) by checking the checkboxes Samy Microsoft ASP .NET 0 21st Jul 2006 06:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:55 AM.