CheckBoxList Select All or None Checkboxes

Here is an very easy client-side javascript to check or uncheck all your checkboxes from your CheckBoxList at once:

<script language="javascript" type="text/javascript">function Select(Select){
for (var n=0; n < document.forms[0].length; n++) if (document.forms[0].elements[n].type==’checkbox’)
document.forms[0].elements[n].checked=Select; return false; }</script>
Select <a href="#" onclick="javascript:Select(true)">All</a> | <a href="#" onclick="javascript:Select(false)">None</a>

Just put it at the top of your ASP.NET page and you’re set. It won’t get any easier than that ;)

9 comments ↓

#1 dupls on 08.01.07 at 8:30 pm

I have several checkBox ‘s on a page and only want to have a checkall box on a check uncheck on a group of them. I’ve made a validataiongroup of the ones I want to have this feature but don’t know how to set the code in place for it to work.

#2 Yanna on 08.10.07 at 12:19 pm

Correction: I use master pages. Where would i put the script part?

#3 andreas.kraus on 08.10.07 at 12:27 pm

Hi Yanna,

define a ContentPlaceHolder within the head tag of your MasterPage and add the script on the particular .aspx site to that ContentPlaceHolder.

Another way would be to add it via this.Header.Controls.Add(); in Codebehind.

hth

#4 Yanna on 08.10.07 at 2:38 pm

Thank you so much for your prompt response! I’ll give it a try!

#5 Simon Thomson on 10.29.07 at 1:52 pm

Thanks for the script. I had to remove the ‘return false’ part as it stopped after the first checkbox! I also notice that you are using ’checkbox’ which has to be converted to ‘checkbox’ (different quotes)

#6 katerina on 04.22.08 at 2:20 am

Thank you!

#7 Randi on 06.13.08 at 7:11 pm

If you’re doing in with ASP.net and want to do it in the code behind..
in the asp, set “OnSelectedIndexChanged” to the method name you want, in mine it’s “checkboxClicked”.
Then, make sure AutoPostBack is set to true. This makes sure that the check boxes are refreshed everytime it’s clicked.

say you have a list item named “Select All” and a list item named “Select None”.
you check list is called “chklst”

here is your method:

public void comboClicked(Object sender, EventArgs e)
{
for (int i = 0; i < chklst.Items.Count; i++)
{
///this says ‘if select none is selected and the box you’re on isn’t the select none box, set the selected to false.
if(chklst.Items.FindByText(“Select None”).Selected == true && chklst.Items[i] != chklst.Items.FindByText(“Select None”))
{
chklst.Items[i].Selected = false;
}

if(chklst.Items.FindByText(“Select All”.Selected == true)
{
chklst.Items[i].Selected = true;
}

}

#8 Queena on 11.12.08 at 11:45 am

if i want to use this control to certain checkboxlist. how should i do? that means, there is a checkbox_A and a item called “delete all”. if ” deleter all” is selected, the all items beside itself would been enabled. how should i do? thx~

#9 mohan on 02.26.09 at 8:48 am

nothing understand

Leave a Comment