62,074
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1" Text="CheckBox1"></asp:ListItem>
<asp:ListItem Value="2" Text="CheckBox2"></asp:ListItem>
<asp:ListItem Value="3" Text="CheckBox3"></asp:ListItem>
<asp:ListItem Value="4" Text="CheckBox4"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
int count=0;
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
count++;
}
}
if (count == 0)
{
Response.Write("<script>alert('必须选择一项!')</script>");
return;
}
}
int _num = 0;
foreach (Control ct in this.Controls)
{
if(ct is CheckBox)
{
if(((CheckBox)ct).Checked)
{
num++;
}
}
}
if(num > 0)
{
}
else
//提示至少要选择1个选项