From: schapopa on
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 ***
From: Mark Rae [MVP] on
<schapopa> wrote in message news:OS7b2fk9KHA.980(a)TK2MSFTNGP04.phx.gbl...

> 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

From: schapopa on
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 ***
From: Mark Rae [MVP] on
<schapopa> wrote in message news:#HKamyy9KHA.420(a)TK2MSFTNGP02.phx.gbl...

> 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&sourceid=chrome&ie=UTF-8&q=JavaScript+indexOf


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