Editar

Partilhar via


Repeater.DataSource Property

Definition

Gets or sets the data source that provides data for populating the list.

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

Property Value

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

Attributes

Exceptions

The DataSource object specified is not a supported source of data for the Repeater control.

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

Examples

The following example demonstrates how to specify the DataSource of the Repeater control when the page is loaded.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!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 Example</title>
<script runat="server">
       void Page_Load(Object Sender, EventArgs e) {
 
          if (!IsPostBack) {
             ArrayList values = new ArrayList();
 
             values.Add("Apple");
             values.Add("Orange");
             values.Add("Pear");
             values.Add("Banana");
             values.Add("Grape");
 
             // Set the DataSource of the Repeater. 
             Repeater1.DataSource = values;
             Repeater1.DataBind();
          }
       }
    </script>
 
 </head>
 <body>
 
    <h3>Repeater Example</h3>
 
    <form id="form1" runat="server">
 
       <b>Repeater1:</b>
       <br />
         
       <asp:Repeater id="Repeater1" runat="server">
             
          <HeaderTemplate>
             <table border="1">
          </HeaderTemplate>
 
          <ItemTemplate>
             <tr>
                <td> <%# Container.DataItem %> </td>
             </tr>
          </ItemTemplate>
             
          <FooterTemplate>
             </table>
          </FooterTemplate>
             
       </asp:Repeater>
       <br />
         
    </form>
 </body>
 </html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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 Example</title>
<script runat="server">

        Sub Page_Load(Sender As Object, e As EventArgs)
            
            If Not IsPostBack Then
                Dim values As New ArrayList()
                
                values.Add("Apple")
                values.Add("Orange")
                values.Add("Pear")
                values.Add("Banana")
                values.Add("Grape")
                
                ' Set the DataSource of the Repeater. 
                Repeater1.DataSource = values
                Repeater1.DataBind()
            End If
        End Sub
        
    </script>
 
 </head>
 <body>
 
    <h3>Repeater Example</h3>
 
    <form id="form1" runat="server">
 
       <b>Repeater1:</b>
       <br />
         
       <asp:Repeater id="Repeater1" runat="server">
             
          <HeaderTemplate>
             <table border="1">
          </HeaderTemplate>
 
          <ItemTemplate>
             <tr>
                <td> <%# Container.DataItem %> </td>
             </tr>
          </ItemTemplate>
             
          <FooterTemplate>
             </table>
          </FooterTemplate>
             
       </asp:Repeater>
       <br />
         
    </form>
 </body>
 </html>

Remarks

Use this property to specify the source of data to populate the Repeater control. The DataSource can be any System.Collections.IEnumerable collection such as a System.Data.DataView for accessing databases, a System.Collections.ArrayList, or an array, or an IListSource object. When you set the DataSource property you must manually write the code to bind to the data source.

If the data source specified by the DataSource property contains multiple sources of data, use the DataMember property to specify the specific source to bind to the control. For example, if you have a System.Data.DataSet with multiple tables, you must specify which table to bind to the control. After you have specified the data source, use the DataBind method to bind the data source to the control.

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 Repeater control automatically binds to the specified data source control. You do not need to write code that explicitly calls the DataBind method unless you dynamically change properties of the Repeater control.

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 is thrown.

Applies to

See also