dポイントプレゼントキャンペーン実施中!

複数チェックボックスの判定について教えてください。

こんな感じになっているのですがもし「2つ以上」チェックがついていたらという基準を設けたいのですが
どのように書いたらいいでしょうか?

<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox4" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox7" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox5" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox8" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="CheckBox3" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox6" runat="server" />
</td>
<td>
<asp:CheckBox ID="CheckBox9" runat="server" />
</td>

A 回答 (1件)

aspx は似たような感じで


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio …
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無題のページ</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:CheckBox ID="CheckBox3" runat="server" />
<asp:CheckBox ID="CheckBox4" runat="server" />
<asp:CheckBox ID="CheckBox5" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
<p><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></p>
</form>
</body>
</html>

で、Default.aspx.vb のほうは
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim chkCount As Integer = 0
For Each ctrl In Me.form1.Controls
If TypeOf ctrl Is CheckBox Then
Dim chkBox As CheckBox = DirectCast(ctrl, CheckBox)
If chkBox.Checked Then
chkCount += 1
End If
End If
Next
If chkCount <> 0 Then
Me.Label1.Text = chkCount.ToString() & " 個チェックがついています"
End If
End Sub
End Class

でどうでしょう。
    • good
    • 0
この回答へのお礼

ありがとうございました
参考にさせていただきます!

お礼日時:2010/05/21 00:37

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!