다음을 통해 공유


CheckBoxList 생성자

정의

CheckBoxList 클래스의 새 인스턴스를 초기화합니다.

public:
 CheckBoxList();
public CheckBoxList ();
Public Sub New ()

예제

다음 코드 예제를 만들고 컨트롤의 새 인스턴스를 초기화 하는 방법을 보여 줍니다 CheckBoxList .

참고

다음 코드 샘플 단일 파일 코드 모델을 사용 하 고 코드 숨김 파일에 직접 복사 하는 경우 제대로 작동 하지 않을 수 있습니다. 이 코드 샘플.aspx 확장명이 있는 빈 텍스트 파일에 복사 해야 합니다. Web Forms 코드 모델에 대 한 자세한 내용은 참조 하세요. ASP.NET Web Forms 페이지 코드 모델합니다.


<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> CheckBoxList Constructor Example </title>
<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 id="form1" 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="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> CheckBoxList Constructor Example </title>
<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 id="form1" 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>

설명

이 생성자를 사용 하 여 만들고의 새 인스턴스를 초기화 하는 CheckBoxList 클래스입니다.

적용 대상

추가 정보