【发布时间】:2017-07-20 01:55:19
【问题描述】:
我的网页上有一个CheckBox 和一个CheckBox 列表。
如果选择了CheckBox,则应选择CheckBoxList 中的所有CheckBoxes,如果未选中CheckBox,则同样应取消选择CheckBox 中的所有CheckBoxes(未选中)。
.aspx 代码
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Item A</asp:ListItem>
<asp:ListItem>Item B</asp:ListItem>
<asp:ListItem>Item C</asp:ListItem>
<asp:ListItem Selected="True">Item D</asp:ListItem>
<asp:ListItem>Item E</asp:ListItem>
<asp:ListItem>Item F</asp:ListItem>
<asp:ListItem>Item G</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBox ID="allChkBox" Text="Select all" runat="server"
oncheckedchanged="allChkBox_CheckedChanged" />
我尝试过这样做,但它不起作用:
bool prevSelection = false;
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
if (!prevSelection)
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = true;
}
}
else
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = false;
}
}
prevSelection = !prevSelection;
}
【问题讨论】:
-
你能提供你的aspx代码吗?
-
好的.. 其他人提供了答案。我建议..您应该在客户端 javascript 中执行此操作,而无需回发。
标签: c# asp.net checkbox checkboxlist