how to check and uncheck all asp.net checkbox on single checkbox check?
how to check and uncheck all asp.net checkbox on single checkbox check ?
I have 5 asp.net checkbox on webform i want when 1 single checkbox checked then all checkbox will be cc开发者_运维知识库hecked and if checkbox is unchecked then all checkb ox will be unchecked ..
a little code snippet :
<asp:CheckBoxList ID="chkStatu" runat="server" RepeatDirection="Horizontal">
</asp:CheckBoxList>
<hr />
<asp:CheckBox ID="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this,'chkStatu');"
Text="SelectAll" />
and js
function SelectAllCheckboxes(spanChk,str)
{
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
var control;
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
{
control =elm[i].id.indexOf(str);
if(elm[i].checked!=xState && control != -1)
{
elm[i].click();
}
}
}
'Check or UnCheck all the checkboxes based on header checkbox and change row color
Protected Sub ckHeader_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chHeader As CheckBox
Dim chSelect As CheckBox
Dim cHeader As Boolean
Dim count As Integer
Dim gvr As DataGridItem
count = -1
Try
chHeader = CType(CMSgrid.Controls(0).Controls(1).FindControl("ckHeader"), CheckBox)
cHeader = chHeader.Checked
For Each gvr In CMSgrid.Items
count = count + 1
chSelect = CType(gvr.FindControl("ckSelect"), CheckBox)
If (cHeader = True) Then
gvr.BackColor = Color.Gold
gvr.ForeColor = Color.Black
chSelect.Checked = True
Else
If (count Mod 2) = 0 Then
gvr.BackColor = Color.LightGoldenrodYellow
gvr.ForeColor = Color.Black
Else
gvr.BackColor = Color.PaleGoldenrod
gvr.ForeColor = Color.Black
End If
chSelect.Checked = False
End If
Next
Catch ex As Exception
UserMsgBox(ex.Message)
WriteToLog(ex.Message)
End Try
End Sub
Where chHeader
indicates the main checkbox.
By inspecting the above code, hopefully you can understand how to make it.
精彩评论