Rediger

Del via


Repeater.DataSourceID Property

Definition

Gets or sets the ID property of the data source control that the Repeater 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 ID property of the data source control.

Exceptions

The data source cannot be resolved for one of the following reasons:

Examples

The following code example demonstrates how to set the DataSourceID property to the ID property of a SqlDataSource control. When the page is loaded, the Repeater control binds to the data source that is 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>
    <title>Repeater.DataSourceID Property Example</title>
</head>

  <body>
    <form id="Form1" runat="server">
        
      <h3>Repeater.DataSourceID Property Example</h3>
      
      <asp:repeater id="Repeater1"       
        datasourceid="SqlDataSource1"
        runat="server">
        
        <headertemplate>
          <table border="1">
            <tr>
              <td><b>Product ID</b></td>
              <td><b>Product Name</b></td>
            </tr>
        </headertemplate>
          
        <itemtemplate>
          <tr>
            <td> <%# Eval("ProductID") %> </td>
            <td> <%# Eval("ProductName") %> </td>
          </tr>
        </itemtemplate>
          
        <footertemplate>
          </table>
        </footertemplate>
      </asp:repeater>
        
            <asp:sqldatasource id="SqlDataSource1"          
            connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" 
        selectcommand="SELECT ProductID, ProductName FROM [Products] Where ProductID <= 10"
        runat="server">
      </asp:sqldatasource>
               
    </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>
    <title>Repeater.DataSourceID Property Example</title>
</head>

  <body>
    <form id="Form1" runat="server">
        
      <h3>Repeater.DataSourceID Property Example</h3>
      
      <asp:repeater id="Repeater1"       
        datasourceid="SqlDataSource1"
        runat="server">
        
        <headertemplate>
          <table border="1">
            <tr>
              <td><b>Product ID</b></td>
              <td><b>Product Name</b></td>
            </tr>
        </headertemplate>
          
        <itemtemplate>
          <tr>
            <td> <%# Eval("ProductID") %> </td>
            <td> <%# Eval("ProductName") %> </td>
          </tr>
        </itemtemplate>
          
        <footertemplate>
          </table>
        </footertemplate>
      </asp:repeater>
        
            <asp:sqldatasource id="SqlDataSource1"          
            connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" 
        selectcommand="SELECT ProductID, ProductName FROM [Products] Where ProductID <= 10"
        runat="server">
      </asp:sqldatasource>
               
    </form>      
  </body>
</html>

Remarks

Use the DataSourceID property to access the ID property of the data source control that the Repeater control should use to retrieve its data source. The data source control that is 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 Repeater control that references it or in a parent control of the Repeater control. When you specify a value for the DataSourceID property, the Repeater control binds to the specified data source control. You do not need to write code that explicitly calls the DataBind method.

Alternatively, you can use the DataSource property to specify the source of values to bind to the Repeater control. The data source must be a collection that implements the System.Collections.IEnumerable interface (such as the System.Data.DataView or System.Collections.ArrayList object) or the IListSource interface. When you set the DataSource property, you must write the code to perform the data binding.

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

The value of this property is stored in view state.

Applies to

See also