Share via


DropDownList 웹 서버 컨트롤 선언 구문

업데이트: 2007년 11월

사용자가 드롭다운 목록에서 하나만 선택할 수 있도록 합니다. 드롭다운 목록에 항목을 원하는 만큼 추가할 수 있습니다.

<asp:DropDownList
    AccessKey="string"
    AppendDataBoundItems="True|False"
    AutoPostBack="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    CssClass="string"
    DataMember="string"
    DataSource="string"
    DataSourceID="string"
    DataTextField="string"
    DataTextFormatString="string"
    DataValueField="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDataBound="DataBound event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedIndexChanged="SelectedIndexChanged event handler"
    OnTextChanged="TextChanged event handler"
    OnUnload="Unload event handler"
    runat="server"
    SelectedIndex="integer"
    SelectedValue="string"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
>
            <asp:ListItem
                Enabled="True|False"
                Selected="True|False"
                Text="string"
                Value="string"
            />
</asp:DropDownList>

설명

DropDownList 컨트롤을 사용하면 한 번에 하나의 항목만 선택할 수 있는 드롭다운 목록 컨트롤을 만들 수 있습니다. BorderColor, BorderStyleBorderWidth 속성을 설정하여 DropDownList 컨트롤의 모양을 제어할 수 있습니다.

DropDownList 컨트롤에 나타나는 항목을 지정하려면 각 항목에 대한 ListItem 요소를 DropDownList 컨트롤의 여는 태그와 닫는 태그 사이에 추가합니다.

DropDownList 컨트롤에서는 데이터 바인딩도 지원합니다. 컨트롤을 데이터 소스에 바인딩하려면 먼저 컨트롤에 표시할 항목이 포함된 데이터 소스(예: ArrayList)를 만듭니다. 그런 다음 DataBind 메서드를 사용하여 데이터 소스를 DropDownList 컨트롤에 바인딩합니다. DataTextFieldDataValueField 속성을 사용하면 컨트롤에 있는 각 목록 항목의 Text 속성과 Value 속성에 각각 바인딩할 데이터 소스 필드를 지정할 수 있습니다. 그러면 DropDownList 컨트롤에서는 데이터 소스에서 가져온 정보를 표시합니다.

SelectedIndex 속성을 사용하면 DropDownList 컨트롤에서 사용자가 선택한 항목의 인덱스를 프로그래밍 방식으로 확인할 수 있습니다. 그런 다음 해당 인덱스를 사용하여 컨트롤의 Items 컬렉션에서 선택된 항목을 검색할 수 있습니다.

DropDownList 웹 서버 컨트롤의 속성과 이벤트에 대한 자세한 내용은 DropDownList 설명서를 참조하십시오.

예제

다음 코드 예제에서는 네 개의 항목이 포함된 DropDownList 컨트롤을 만드는 방법을 보여 줍니다.

<%@ 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" >
   <script runat="server" >

      Sub Selection_Change(sender As Object, e As EventArgs)

         ' Set the background color for days in the Calendar control
         ' based on the value selected by the user from the
         '  DropDownList control.
         Calendar1.DayStyle.BackColor = _
             System.Drawing.Color.FromName(ColorList.SelectedItem.Value)

      End Sub

   </script>

<head runat="server">
    <title> DropDownList Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3> DropDownList Example </h3>

      Select a background color for days in the calendar.

      <br /><br /> 

      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Background color:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>   


   </form>

</body>
</html>

<%@ 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" >
   <script runat="server" >

      void Selection_Change(Object sender, EventArgs e)
      {

         // Set the background color for days in the Calendar control
         // based on the value selected by the user from the 
         // DropDownList control.
         Calendar1.DayStyle.BackColor = 
             System.Drawing.Color.FromName(ColorList.SelectedItem.Value);

      }

   </script>

<head runat="server">
    <title> DropDownList Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3> DropDownList Example </h3>

      Select a background color for days in the calendar.

      <br /><br /> 

      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Background color:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>


   </form>

</body>
</html>

다음 코드 예제에서는 선언적 데이터 바인딩을 통해 DropDownList 컨트롤을 채우는 방법을 보여 줍니다.

<%@ Page Language="VB"%>
<%@ Import Namespace="System.Drawing" %>
<!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>DropDownList Declarative DataBinding Sample</title>
    <script runat="server">
        Sub Selection_Change(ByVal sender As Object, ByVal e As EventArgs)
            '' Set the background color for days in the Calendar control
            '' based on the value selected by the user from the 
            '' DropDownList control.
            Calendar1.DayStyle.BackColor = _
                Color.FromName(ColorList.SelectedItem.Value)
        End Sub
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:XmlDataSource ID="Colors" runat="server">
        <Data>
            <Colors>
                <Color ColorID="White" ColorName="White" />
                <Color ColorID="Silver" ColorName="Silver" />
                <Color ColorID="DarkGray" ColorName="Dark Gray" />
                <Color ColorID="Khaki" ColorName="Khaki" />
                <Color ColorID="DarkKhaki" ColorName="Dark Khaki" />
            </Colors>
        </Data>
    </asp:XmlDataSource>
    Colors: <asp:DropDownList ID="ColorList" 
                DataSourceID="Colors" 
                DataTextField="ColorName" 
                DataValueField="ColorID" 
                runat="server" 
                AutoPostBack="true" 
                OnSelectedIndexChanged="Selection_Change" />
    <p />
    <asp:Calendar ID="Calendar1" runat="server" />
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<!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>DropDownList Declarative DataBinding Sample</title>
    <script runat="server">
        void Selection_Change(Object sender, EventArgs e)
        {

            // Set the background color for days in the Calendar control
            // based on the value selected by the user from the 
            // DropDownList control.
            Calendar1.DayStyle.BackColor = 
                Color.FromName(ColorList.SelectedItem.Value);

        }   

</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:XmlDataSource ID="Colors" runat="server">
        <Data>
            <Colors>
                <Color ColorID="White" ColorName="White" />
                <Color ColorID="Silver" ColorName="Silver" />
                <Color ColorID="DarkGray" ColorName="Dark Gray" />
                <Color ColorID="Khaki" ColorName="Khaki" />
                <Color ColorID="DarkKhaki" ColorName="Dark Khaki" />
            </Colors>
        </Data>
    </asp:XmlDataSource>
    Colors: <asp:DropDownList ID="ColorList" 
                DataSourceID="Colors" 
                DataTextField="ColorName" 
                DataValueField="ColorID" 
                runat="server" 
                AutoPostBack="true" 
                OnSelectedIndexChanged="Selection_Change" />
    <p />
    <asp:Calendar ID="Calendar1" runat="server" />
    </form>
</body>
</html>

다음 코드 예제에서는 프로그래밍 방식의 데이터 바인딩을 통해 DropDownList 컨트롤을 만드는 방법을 보여 줍니다.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!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" >
   <script runat="server" >

      Sub Selection_Change(sender as Object, e As EventArgs)

         ' Set the background color for days in the Calendar control 
         ' based on the value selected by the user from the
         ' DropDownList control.
         Calendar1.DayStyle.BackColor = _
             System.Drawing.Color.FromName(ColorList.SelectedItem.Value)

      End Sub

      Sub Page_Load(sender as Object, e As EventArgs)

         ' Load data for the DropDownList control only once, when the 
         ' page is first loaded.
         If Not IsPostBack Then

            ' Specify the data source and field names for the Text 
            ' and Value properties of the items (ListItem objects)
            ' in the DropDownList control.
            ColorList.DataSource = CreateDataSource()
            ColorList.DataTextField = "ColorTextField"
            ColorList.DataValueField = "ColorValueField"

            ' Bind the data to the control.
            ColorList.DataBind()

            ' Set the default selected item, if desired.
            ColorList.SelectedIndex = 0

         End If

      End Sub

      Function CreateDataSource() As ICollection 

         ' Create a table to store data for the DropDownList control.
         Dim dt As DataTable = New DataTable()

         ' Define the columns of the table.
         dt.Columns.Add(new DataColumn("ColorTextField", GetType(String)))
         dt.Columns.Add(new DataColumn("ColorValueField", GetType(String)))

         ' Populate the table with sample values.
         dt.Rows.Add(CreateRow("White", "White", dt))
         dt.Rows.Add(CreateRow("Silver", "Silver", dt))
         dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt))
         dt.Rows.Add(CreateRow("Khaki", "Khaki", dt))
         dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt))

         ' Create a DataView from the DataTable to act as the data source
         ' for the DropDownList control.
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function

      Function CreateRow(Text As String, Value As String, dt As DataTable) As DataRow 

         ' Create a DataRow using the DataTable defined in the 
         ' CreateDataSource method.
         Dim dr As DataRow = dt.NewRow()

         ' This DataRow contains the ColorTextField and ColorValueField 
         ' fields, as defined in the CreateDataSource method. Set the 
         ' fields with the appropriate value. Remember that column 0 
         ' is defined as ColorTextField, and column 1 is defined as 
         ' ColorValueField.
         dr(0) = Text
         dr(1) = Value

         Return dr

      End Function

   </script>

<head runat="server">
    <title> DropDownList Data Binding Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3> DropDownList Data Binding Example </h3>

      Select a background color for days in the calendar.

      <br /><br /> 

      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Background color:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server"/>

            </td>

         </tr>

      </table>   


   </form>

</body>
</html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!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" >
   <script runat="server" >

      void Selection_Change(Object sender, EventArgs e)
      {

         // Set the background color for days in the Calendar control
         // based on the value selected by the user from the 
         // DropDownList control.
         Calendar1.DayStyle.BackColor = 
             System.Drawing.Color.FromName(ColorList.SelectedItem.Value);

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Load data for the DropDownList control only once, when the 
         // page is first loaded.
         if(!IsPostBack)
         {

            // Specify the data source and field names for the Text 
            // and Value properties of the items (ListItem objects) 
            // in the DropDownList control.
            ColorList.DataSource = CreateDataSource();
            ColorList.DataTextField = "ColorTextField";
            ColorList.DataValueField = "ColorValueField";

            // Bind the data to the control.
            ColorList.DataBind();

            // Set the default selected item, if desired.
            ColorList.SelectedIndex = 0;

         }

      }

      ICollection CreateDataSource() 
      {

         // Create a table to store data for the DropDownList control.
         DataTable dt = new DataTable();

         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
         dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

         // Populate the table with sample values.
         dt.Rows.Add(CreateRow("White", "White", dt));
         dt.Rows.Add(CreateRow("Silver", "Silver", dt));
         dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
         dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
         dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));

         // Create a DataView from the DataTable to act as the data source
         // for the DropDownList control.
         DataView dv = new DataView(dt);
         return dv;

      }

      DataRow CreateRow(String Text, String Value, DataTable dt)
      {

         // Create a DataRow using the DataTable defined in the 
         // CreateDataSource method.
         DataRow dr = dt.NewRow();

         // This DataRow contains the ColorTextField and ColorValueField 
         // fields, as defined in the CreateDataSource method. Set the 
         // fields with the appropriate value. Remember that column 0 
         // is defined as ColorTextField, and column 1 is defined as 
         // ColorValueField.
         dr[0] = Text;
         dr[1] = Value;

         return dr;

      }

   </script>

<head runat="server">
    <title> DropDownList Data Binding Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3> DropDownList Data Binding Example </h3>

      Select a background color for days in the calendar.

      <br /><br /> 

      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Background color:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server"/>

            </td>

         </tr>

      </table>   


   </form>

</body>
</html>

참고 항목

개념

ASP.NET 서버 컨트롤

참조

DropDownList 웹 서버 컨트롤 개요

DropDownList

기타 리소스

웹 서버 컨트롤 구문