HtmlSelect.DataSourceID Property

Definition

Gets or sets the ID property of the data source control that the HtmlSelect control should use to retrieve its data source.

public:
 virtual property System::String ^ DataSourceID { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataSourceID { get; set; }
member this.DataSourceID : string with get, set
Public Overridable Property DataSourceID As String

Property Value

The programmatic identifier assigned to the data source control. The default value is an empty string (""), which indicates that the property has not been set.

Exceptions

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 DataSourceID property to specify the data source for an HtmlSelect control. The DataSourceID property is set to the ID property of the SqlDataSource control used to retrieve the data. When the page is loaded, the HtmlSelect control automatically binds to the data source specified by the SqlDataSource control and the data is displayed to the user.

<%@ page language="C#" %>

<!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 id="Head1" runat="server">
    <title>HtmlSelect.DataSourceID</title>
</head>
<body>
<form id="Form1" runat="server">
  <div>

    <h3> HtmlSelect.DataSourceID Example </h3>

    <p>Select an item from the list</p>

    <select id="Select1"
      name="Select1"
      datasourceid="SqlDataSource1"
      datatextfield="ProductName"
      runat="server">
    </select>

    <asp:sqldatasource id="SqlDataSource1"          
      connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>

  </div>
</form>
</body>
</html>
<%@ page language="VB" %>

<!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 id="Head1" runat="server">
    <title>HtmlSelect.DataSourceID</title>
</head>
<body>
<form id="Form1" runat="server">
  <div>

    <h3> HtmlSelect.DataSourceID Example </h3>

    <p>Select an item from the list</p>

    <select id="Select1"
      name="Select1"
      datasourceid="SqlDataSource1"
      datatextfield="ProductName"
      runat="server">
    </select>

    <asp:sqldatasource id="SqlDataSource1"          
      connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>

  </div>
</form>
</body>
</html>

Remarks

Use the DataSourceID property to set or access the ID property of the data source control that the HtmlSelect control should use to retrieve its data source. The data source control referenced by the DataSourceID property can be any control that implements the IDataSource interface. The data source control must exist either in the same naming container as the HtmlSelect control that references it or in a parent control of the HtmlSelect control. When you specify a value for this property, the HtmlSelect control automatically binds to the specified data source control. You do not need to write code that explicitly calls the DataBind method.

Alternately, you can use the DataSource property to specify the source of values to bind to an HtmlSelect control. The data source must be a collection that implements 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 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

See also