Share via


HtmlSelect.DataSource 屬性

定義

取得或設定要繫結至 HtmlSelect 控制項的資訊來源。

public:
 virtual property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataSource { get; set; }
member this.DataSource : obj with get, set
Public Overridable Property DataSource As Object

屬性值

Object

IEnumerableIListSource,包含用來提供資料給這個控制項的值集合。 預設值是 null

例外狀況

指定的資料來源並不相容於 IEnumerableIListSource,而且也不屬於 null

無法解析資料來源,因為同時為 DataSource 屬性和 DataSourceID 屬性指定了值。

範例

下列程式碼範例示範如何使用 DataSource 屬性來指定要系結至 HtmlSelect 控制項的資訊來源。

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

<!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>
    <title> HtmlSelect Example </title>
<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 id="form1" 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="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!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>
    <title> HtmlSelect Example </title>
<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 id="form1" 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>

備註

DataSource使用 屬性指定要系結至 控制項的 HtmlSelect 資料來源。 資料來源必須是實 System.Collections.IEnumerable 作 介面 (的集合,例如 System.Data.DataViewSystem.Collections.ArrayListSystem.Collections.Generic.List<T>) 或 IListSource 介面。 當您設定 DataSource 屬性時,您必須手動撰寫程式碼來執行資料系結。

如果資料來源包含多個資料集,例如 System.Data.DataSet 具有多個資料表的物件,請使用 DataMember 屬性來指定要系結至控制項的資料集。

您可以分別設定 DataTextField 和 屬性,以指定資料來源中的哪些欄位,以系結至 ListItem.Text ListItem.Value 控制項中每個專案的 和 DataValueField 屬性。

或者,您可以使用 DataSourceID 屬性自動系結至資料來源控制項所代表的資料來源。 當您設定 DataSourceID 屬性時,資料清單控制項會自動系結至指定的資料來源控制項。 您不需要撰寫明確呼叫 方法的程式 DataBind 代碼。

如果同時為 DataSource 屬性和 DataSourceID 屬性指定值,則 ASP.NET 無法解析資料來源,並 System.Web.HttpException 擲回例外狀況。

適用於

另請參閱