HtmlSelect.DataSource Property

Definition

Gets or sets the source of information to bind to the HtmlSelect control.

public virtual object DataSource { get; set; }

Property Value

An IEnumerable or IListSource that contains a collection of values used to supply data to this control. The default value is null.

Exceptions

The specified data source is not compatible with either IEnumerable or IListSource, and it is not null.

The data source cannot be resolved because a value is specified for both the DataSource property and the DataSourceID property.

Examples

The following code example demonstrates how to use the DataSource property to specify the source of information to bind to the HtmlSelect control.

<%@ 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>

Remarks

Use the DataSource property to specify the data source to bind to the HtmlSelect control. A data source must be a collection that implements either the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Generic.List<T>) or the IListSource interface. When you set the DataSource property, you must manually write the code to perform data binding.

If the data source contains multiple sets of data, such as a System.Data.DataSet object with multiple tables, use the DataMember property to specify which data set to bind to the control.

You can specify which fields from the data source to bind to the ListItem.Text and ListItem.Value properties of each item in the control by setting the DataTextField and DataValueField properties, respectively.

Alternately, you can use the DataSourceID property to automatically bind to a data source represented by a data source control. When you set the DataSourceID property, the data listing control automatically binds to the specified data source control. You do not need to write code that explicitly calls the DataBind method.

If values are specified for both the DataSource property and the DataSourceID property, ASP.NET is not able to resolve the data source and a System.Web.HttpException exception is thrown.

Applies to

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also