DropDownList コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DropDownList コンストラクタの意味・解説 

DropDownList コンストラクタ

DropDownList クラス新しインスタンス初期化します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

解説解説
使用例使用例

DropDownList クラス新しインスタンス作成および初期化する方法次のコード例示します

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

<html>
   <script runat="server" >
  
      Sub Selection_Change(sender as Object,
 e As EventArgs)

         ' Retrieve the DropDownList control from the Controls 
         ' collection of the PlaceHolder control.
         Dim DropList As DropDownList = _
             CType(Place.FindControl("ColorList"),
 DropDownList) 

         ' 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(DropList.SelectedItem.Value)

      End Sub

      Sub Page_Load(sender as Object,
 e As EventArgs)

         ' Create a DropDownList control.
         Dim DropList As DropDownList = New
 DropDownList()

         ' Set the properties for the DropDownList control.
         DropList.ID = "ColorList"
         DropList.AutoPostBack = True

         ' Manually register the event-handling method for the 
         ' SelectedIndexChanged event.
         AddHandler DropList.SelectedIndexChanged, AddressOf
 Selection_Change

         ' Because the DropDownList control is created dynamically each
 
         ' time the page is loaded, the data must be bound to the
         ' control each time the page is refreshed.

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

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

         ' Set the default selected item when the page is first loaded.
         If Not IsPostBack Then
        
            DropList.SelectedIndex = 0
         
         End If

         ' Add the DropDownList control to the Controls collection of
 
         ' the PlaceHolder control.
         Place.Controls.Add(DropList)

      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>
  
<body>

   <form runat="server">
  
      <h3> DropDownList Constructor 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:PlaceHolder id="Place"
                    runat="server"/>

            </td>

         </tr>
  
   </form>

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

<html>
   <script runat="server" >
  
      void Selection_Change(Object sender, EventArgs e)
      {

         // Retrieve the DropDownList control from the Controls  
         // collection of the PlaceHolder control.
         DropDownList DropList = 
             (DropDownList)Place.FindControl("ColorList"); 

         // 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(DropList.SelectedItem.Value);

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Create a DropDownList control.
         DropDownList DropList = new DropDownList();

         // Set the properties for the DropDownList control.
         DropList.ID = "ColorList";
         DropList.AutoPostBack = true;

         // Manually register the event-handling method for the 
         // SelectedIndexChanged event.
         DropList.SelectedIndexChanged += 
             new EventHandler(this.Selection_Change);

         // Because the DropDownList control is created dynamically
 each
         // time the page is loaded, the data must be bound to the
         // control each time the page is refreshed.

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

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

         // Set the default selected item when the page is first loaded.
         if(!IsPostBack)
         {  
            DropList.SelectedIndex = 0;
         }

         // Add the DropDownList control to the Controls collection
 of 
         // the PlaceHolder control.
         Place.Controls.Add(DropList);

      }

      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>
  
<body>

   <form runat="server">
  
      <h3> DropDownList Constructor 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:PlaceHolder id="Place"
                    runat="server"/>

            </td>

         </tr>
  
   </form>

</body>
</html>
 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DropDownList コンストラクタ」の関連用語

DropDownList コンストラクタのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DropDownList コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS