How do I check all the checkboxes on a page?
I am trying to validate all checkboxes on a web form page (aspx) that is inside the master page, depending on the checkbox id. The checkboxes are generated dynamically, so I only know the prefix to find it. So, I need to find these checkboxes by iterating over the controls on the page.
Here is the code where the check should take place:
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim oCheckbox As System.Web.UI.WebControls.CheckBox = Nothing
Dim oControl As Control = Nothing
For Each oControl In Me.Controls
If oControl IsNot Nothing Then
If TypeOf oControl Is System.Web.UI.WebControls.CheckBox Then
oCheckbox = oControl
If oCheckbox.Text.StartsWith("ClientCheckBox_") Then
oCheckbox.Checked = True
End If
End If
End If
Next
End Sub
source to share
Here is a non-jQuery example of how to do this client side. Let me know if you need more help to put this example into practice.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="jquery-1.3.2-vsdoc2.js"></script>
<script type="text/javascript">
function selectDeselect(button) {
var checked = (button.value === 'Select All');
var checkboxes = document.getElementsByName('myCheckBoxGroup');
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = checked;
}
button.value = (checked) ? 'Deselect All' : 'Select All';
}
</script>
<style type="text/css">
</style>
</head>
<body>
<input type="button" value="Select All" onclick="selectDeselect(this);" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
<input type="checkbox" name="myCheckBoxGroup" />
</body>
</html>
source to share
The control collection is not deep or recursive — it only contains immediate child controls. You need to do this operation recursively if you can't be sure where the controls might end up, or if you are sure, make sure you target a specific container that you know contains all of your checkboxes.
For recursive search, try something like:
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
CheckCheckBoxes(Me)
End Sub
Protected Sub CheckCheckBoxes(ByVal ctrl as Control)
For Each childCtrl in ctrl.Controls
If TypeOf childCtrl Is CheckBox AndAlso childCtrl.Text.StartsWith("ClientCheckBox_") Then
CType(childCtrl, CheckBox).Checked = True
Else
CheckCheckBoxes(childCtrl)
End If
Next
End Sub
source to share
In situations where you need controls to interact with each other between object hierarchies, you are better off using the observer pattern like this (can be adapted as server-side or client-side code as needed):
Start with a simple class to represent your notifier:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
public class CheckAllManager
{
public static CheckAllManager Instance
{
get
{
if (HttpContext.Current.Session["CheckAllManager"] == null)
{
HttpContext.Current.Session["CheckAllManager"] = new CheckAllManager();
}
return (CheckAllManager)HttpContext.Current.Session["CheckAllManager"];
}
}
private HashSet<CheckBox> checkboxes = new HashSet<CheckBox>();
private CheckAllManager() { }
public void Register(CheckBox checkbox)
{
checkboxes.Add(checkbox);
}
public void Unregister(CheckBox checkbox)
{
checkboxes.Remove(checkbox);
}
public void CheckAll(bool checkState)
{
foreach (CheckBox checkbox in checkboxes)
{
checkbox.Checked = checkState;
}
}
}
Obviously, you should adapt this class to your needs, but its very simple:
- Call
CheckAllManager.Instance.Register(checkbox)
when creating a checkbox. - Call
CheckAllManager.Instance.Unregister(checkbox)
when stacking the box. - Call
CheckAllManager.Instance.CheckAll(state)
to set the state of all checkboxes registered in the class.
This technique works without relying on flags to be named in a specific way or recursively on each object. A collection of controls.
source to share
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
CheckAll(Me, "ClientCheckBox_")
End Sub
Sub CheckAll(parent as Control, startsWith as String)
Dim cb as CheckBox = TryCast(parent, CheckBox)
If cb IsNot Nothing AndAlso cb.Text.StartsWith(startsWith) Then
cb.Checked = True
End If
For Each c as Control in parent
CheckAll(c, startsWith)
Next
End Sub
source to share