CheckBoxList コンストラクタ
アセンブリ: System.Web (system.web.dll 内)



CheckBoxList コントロールの新しいインスタンスを作成および初期化する方法のコード例を次に示します。
![]() |
---|
次のコード サンプルはシングルファイル コード モデルを使用しており、分離コード ファイルに直接コピーされた場合は正常に動作しない可能性があります。このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。Web フォームのコード モデルの詳細については、「ASP.NET Web ページのコード モデル」を参照してください。 |
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Check_Clicked(sender As Object, e As EventArgs) ' Retrieve the CheckBoxList control from the Controls collection ' of the PlaceHolder control. Dim checklist As CheckBoxList = _ CType(Place.FindControl("checkboxlist1"), CheckBoxList) ' Make sure a control was found. If Not checklist Is Nothing Message.Text = "Selected Item(s):<br><br>" ' Iterate through the Items collection of CheckBoxList ' control and display the selected items. Dim i As Integer For i=0 To checklist.Items.Count - 1 If checklist.Items(i).Selected Then Message.Text &= checklist.Items(i).Text & "<br>" End If Next i Else ' Display an error message. Message.Text = "Unable to find CheckBoxList control." End If End Sub Sub Page_Load(sender As Object, e As EventArgs) ' Create a new CheckBoxList control. Dim checklist As CheckBoxList = New CheckBoxList() ' Set the properties of the control. checklist.ID = "checkboxlist1" checklist.AutoPostBack = True checklist.CellPadding = 5 checklist.CellSpacing = 5 checklist.RepeatColumns = 2 checklist.RepeatDirection = RepeatDirection.Vertical checklist.RepeatLayout = RepeatLayout.Flow checklist.TextAlign = TextAlign.Right ' Populate the CheckBoxList control. checklist.Items.Add(New ListItem("Item 1")) checklist.Items.Add(New ListItem("Item 2")) checklist.Items.Add(New ListItem("Item 3")) checklist.Items.Add(New ListItem("Item 4")) checklist.Items.Add(New ListItem("Item 5")) checklist.Items.Add(New ListItem("Item 6")) ' Manually register the event-handling method for the ' SelectedIndexChanged event. AddHandler checklist.SelectedIndexChanged, AddressOf Check_Clicked ' Add the control to the Controls collection of the ' PlaceHolder control. Place.Controls.Add(checklist) End Sub </script> </head> <body> <form runat="server"> <h3> CheckBoxList Constructor Example </h3> Select items from the CheckBoxList. <br><br> <asp:PlaceHolder id="Place" runat="server"/> <br><br> <asp:label id="Message" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Check_Clicked(Object sender, EventArgs e) { // Retrieve the CheckBoxList control from the Controls collection // of the PlaceHolder control. CheckBoxList checklist = (CheckBoxList)Place.FindControl("checkboxlist1"); // Make sure a control was found. if(checklist != null) { Message.Text = "Selected Item(s):<br><br>"; // Iterate through the Items collection of the CheckBoxList // control and display the selected items. for (int i=0; i<checklist.Items.Count; i++) { if (checklist.Items[i].Selected) { Message.Text += checklist.Items[i].Text + "<br>"; } } } else { // Display an error message. Message.Text = "Unable to find CheckBoxList control."; } } void Page_Load(Object sender, EventArgs e) { // Create a new CheckBoxList control. CheckBoxList checklist = new CheckBoxList(); // Set the properties of the control. checklist.ID = "checkboxlist1"; checklist.AutoPostBack = true; checklist.CellPadding = 5; checklist.CellSpacing = 5; checklist.RepeatColumns = 2; checklist.RepeatDirection = RepeatDirection.Vertical; checklist.RepeatLayout = RepeatLayout.Flow; checklist.TextAlign = TextAlign.Right; // Populate the CheckBoxList control. checklist.Items.Add(new ListItem("Item 1")); checklist.Items.Add(new ListItem("Item 2")); checklist.Items.Add(new ListItem("Item 3")); checklist.Items.Add(new ListItem("Item 4")); checklist.Items.Add(new ListItem("Item 5")); checklist.Items.Add(new ListItem("Item 6")); // Manually register the event-handling method for the // SelectedIndexChanged event. checklist.SelectedIndexChanged += new EventHandler(this.Check_Clicked); // Add the control to the Controls collection of the // PlaceHolder control. Place.Controls.Add(checklist); } </script> </head> <body> <form runat="server"> <h3> CheckBoxList Constructor Example </h3> Select items from the CheckBoxList. <br><br> <asp:PlaceHolder id="Place" runat="server"/> <br><br> <asp:label id="Message" runat="server"/> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


CheckBoxList クラス
CheckBoxList メンバ
System.Web.UI.WebControls 名前空間
その他の技術情報
CheckBox Web サーバー コントロールおよび CheckBoxList Web サーバー コントロール
- CheckBoxList コンストラクタのページへのリンク