Just the other day I needed the same functionality for checkboxes in a GridView so wrote a short Javascript function that is registered as a client script block during the Pre_Render event. In the following,
btnSelectAll and btnClearSelected are references to two LinkButtons
gvExpiredPosts is a reference to the GridView
'cbSelected' is the ID of the CheckBox controls placed in a TemplateColumn of the GridView
Here's the code (Note: remove space between java and script):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSelectAll.OnClientClick = "java script:return setAllSelected('" & gvExpiredPosts.ClientID & "', true);"
btnClearSelected.OnClientClick = "java script:return setAllSelected('" & gvExpiredPosts.ClientID & "', false);"
'other code here
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim sb As New StringBuilder
sb.AppendLine("function setAllSelected(gvCtrlID, selected) {")
sb.AppendLine(" var gvCtrl = document.getElementById(gvCtrlID);")
sb.AppendLine(" if (gvCtrl) {")
sb.AppendLine(" var cbs = gvCtrl.getElementsByTagName('input');")
sb.AppendLine(" for (var i=0; i < cbs.length; i++) {")
sb.AppendLine(" var cb = cbs[i];")
sb.AppendLine(" if (cb.type == 'checkbox' && cb.id.indexOf('cbSelected',0) >= 0) {")
sb.AppendLine(" cb.checked = selected;")
sb.AppendLine(" }")
sb.AppendLine(" }")
sb.AppendLine(" return false;")
sb.AppendLine(" }")
sb.AppendLine(" return true;")
sb.AppendLine("}")
If Not Page.ClientScript.IsClientScriptBlockRegistered(Me.GetType, "EPrayer_MngExp") Then
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "EPrayer_MngExp", sb.ToString, True)
End If
End Sub