HtmlSelect.DataSource プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > HtmlSelect.DataSource プロパティの意味・解説 

HtmlSelect.DataSource プロパティ

HtmlSelect コントロールバインドする情報ソース取得または設定します

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

Public Overridable Property
 DataSource As Object
Dim instance As HtmlSelect
Dim value As Object

value = instance.DataSource

instance.DataSource = value
public virtual Object DataSource { get; set;
 }
/** @property */
public Object get_DataSource ()

/** @property */
public void set_DataSource (Object value)

プロパティ
このコントロールデータ提供するために使用する値のコレクション格納している IEnumerable または IListSource。既定値null 参照 (Visual Basic では Nothing) です。

例外例外
例外種類条件

ArgumentException

指定したデータ ソースSystem.Collections.IEnumerable または System.ComponentModel.IListSource互換性がなく、null 参照 (Visual Basic では Nothing) でもありません。

HttpException

DataSource プロパティと DataSourceID プロパティ両方に値が指定されているため、データ ソース解決できません。

解説解説

DataSource プロパティ使用してHtmlSelect コントロールバインドするデータ ソース指定しますデータ ソースは、System.Collections.IEnumerable インターフェイス (System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable など) または IListSource インターフェイス実装するオブジェクトである必要がありますDataSource プロパティ設定する場合は、データ バインディング実行するコード手動記述する必要があります

複数テーブルを持つ System.Data.DataSet オブジェクトなどの複数データ セットデータ ソース格納されている場合は、DataMember プロパティ使用してコントロールバインドするデータ セット指定します

コントロール内の各項目の ListItem.Text プロパティと ListItem.Value プロパティデータ ソースのどのフィールドバインドするかは、DataTextField プロパティと DataValueField プロパティ個別設定することで指定できます

また、DataSourceID プロパティ使用してデータ ソース コントロール表されデータ ソースへのバインディング自動的に行うことができますDataSourceID プロパティ設定すると、データ リスト コントロールは、指定したデータ ソース コントロール自動的にバインドされます。DataBind メソッド明示的に呼び出すコード記述する要はありません。

DataSource プロパティDataSourceID プロパティ両方に値を指定した場合ASP.NETデータ ソース解決できず、System.Web.HttpException 例外スローさます。

使用例使用例

DataSource プロパティ使用してHtmlSelect コントロールバインドする情報ソース指定する方法次のコード例示します

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

<html>

<head>

   <script runat="server">

      Sub Page_Load (sender As Object,
 e As EventArgs)
  
        ' Bind the HtmlSelect control to a data source when the page
 is initially loaded.
        If Not IsPostBack Then
        
           ' Open a connection to the database and run the query.
           ' Note that the connection string may vary depending on your
           ' database server settings.
           Dim ConnectString As String
 = "server=localhost;database=pubs;integrated security=SSPI"
           Dim QueryString As String
 = "select * from authors"

           Dim myConnection As SqlConnection
 = New SqlConnection(ConnectString)
           Dim myCommand As SqlDataAdapter
 = New SqlDataAdapter(QueryString, myConnection)

           ' Create a dataset to store the query results.
           Dim ds As DataSet = New
 DataSet()
           myCommand.Fill(ds, "Authors")

           ' Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds
           Select1.DataTextField = "au_fname"
           Select1.DataValueField = "au_fname"
           Select1.DataBind()
        
        End If

      End Sub

      Sub Button_Click (sender As Object,
 e As EventArgs)
        
         Dim i As Integer

         Label1.Text = "You selected:"

         For i = 0 To Select1.Items.Count -
 1
         
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br> &nbsp;&nbsp;
 - " & Select1.Items(i).Text
            End If

         Next i

      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br>
      Use the Control or Shift key to select
 multiple items. <br><br>

      <select id="Select1"
              Multiple="True" 
              runat="server"/>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

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

<html>

<head>

   <script runat="server">

      void Page_Load (Object sender, EventArgs e)
      {

        // Bind the HtmlSelect control to a data source when the page
 is initially loaded.
        if (!IsPostBack)
        {
      
           // Open a connection to the database and run the query.
           // Note that the connection string may vary depending on
 your
           // database server settings. 
           string ConnectString = "server=localhost;database=pubs;integrated
 security=SSPI";
           string QueryString = "select * from authors";

           SqlConnection myConnection = new SqlConnection(ConnectString);
           SqlDataAdapter myCommand = new SqlDataAdapter(QueryString,
 myConnection);

           // Create a dataset to store the query results.
           DataSet ds = new DataSet();
           myCommand.Fill(ds, "Authors");

           // Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds;
           Select1.DataTextField = "au_fname";
           Select1.DataValueField = "au_fname";
           Select1.DataBind();
        }

      }

      void Button_Click (Object sender, EventArgs e)
      {
       
         // Display the selected items. 
         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count
 - 1; i++)
         {
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; - " + Select1.Items[i].Text;
         }

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br>
      Use the Control or Shift key to select multiple items. <br><br>

      <select id="Select1"
              Multiple="True" 
              runat="server"/>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      function Page_Load (sender : Object, e : EventArgs) : void
      {

        // Bind the HtmlSelect control to a data source when the page
 is initially loaded.
        if (!IsPostBack)
        {
      
           // Open a connection to the database and run the query.
           // Note that the connection string may vary depending on
 your
           // database server settings. 
           var ConnectString : String = "server=localhost;database=pubs;integrated
 security=SSPI";
           var QueryString : String = "select * from authors";

           var myConnection : SqlConnection = new
 SqlConnection(ConnectString);
           var myCommand : SqlDataAdapter = new
 SqlDataAdapter(QueryString, myConnection);

           // Create a dataset to store the query results.
           var ds : DataSet  = new DataSet();
           myCommand.Fill(ds, "Authors");

           // Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds;
           Select1.DataTextField = "au_fname";
           Select1.DataValueField = "au_fname";
           Select1.DataBind();
        }

      }

      function Button_Click (sender : Object, e : EventArgs) :
 void
      {
       
         // Display the selected items. 
         Label1.Text = "You selected:";

         for (var i: int=0;
 i<=Select1.Items.Count - 1; i++)
         {
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; - " + Select1.Items[i].Text;
         }

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br>
      Use the Control or Shift key to select multiple items. <br><br>

      <select id="Select1"
              Multiple="True" 
              runat="server"/>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
HtmlSelect クラス
HtmlSelect メンバ
System.Web.UI.HtmlControls 名前空間
HtmlSelect.DataMember プロパティ
System.Data.DataSet
DataTextField
ListItem.Text
DataValueField
ListItem.Value
DataSourceID
その他の技術情報
HTML サーバー コントロール


このページでは「.NET Framework クラス ライブラリ リファレンス」からHtmlSelect.DataSource プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からHtmlSelect.DataSource プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からHtmlSelect.DataSource プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

「HtmlSelect.DataSource プロパティ」の関連用語

HtmlSelect.DataSource プロパティのお隣キーワード
検索ランキング

   

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



HtmlSelect.DataSource プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS