如何在单一复选框检查中选中并取消选中所有asp.net复选框?
我在webform上有5个asp.net复选框,我希望当选中1个复选框时,将复选所有复选框,如果未选中复选框,则取消选中所有checkb ox ..
答案 0 :(得分:1)
一小段代码:
<asp:CheckBoxList ID="chkStatu" runat="server" RepeatDirection="Horizontal">
</asp:CheckBoxList>
<hr />
<asp:CheckBox ID="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this,'chkStatu');"
Text="SelectAll" />
和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();
}
}
}
答案 1 :(得分:0)
'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
chHeader
表示主要复选框。
通过检查上面的代码,希望你能理解如何制作它。