HtmlSelect.DataSource プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As HtmlSelect Dim value As Object value = instance.DataSource instance.DataSource = value
/** @property */ public Object get_DataSource () /** @property */ public void set_DataSource (Object value)
このコントロールにデータを提供するために使用する値のコレクションを格納している IEnumerable または IListSource。既定値は null 参照 (Visual Basic では Nothing) です。


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> - " & 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> - " + 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> - " + 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>

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からHtmlSelect.DataSource プロパティを検索する場合は、下記のリンクをクリックしてください。

- HtmlSelect.DataSource プロパティのページへのリンク