Repeater.DataSource 屬性

定義

取得或設定提供資料以填入 (Populate) 清單的資料來源。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

屬性值

Object

IEnumerableIListSource 物件,包含用於提供資料給這個控制項的值集合。An IEnumerable or IListSource object that contains a collection of values used to supply data to this control. 預設值是 nullThe default value is null.

屬性

例外狀況

指定的 DataSource 物件不是 Repeater 控制項支援的資料來源。The DataSource object specified is not a supported source of data for the Repeater control.

無法解析資料來源,因為同時為 DataSource 屬性和 DataSourceID 屬性指定了值。The data source cannot be resolved because a value is specified for both the DataSource property and the DataSourceID property.

範例

下列範例示範如何在 DataSource Repeater 載入頁面時指定控制項的。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>
 

備註

您可以使用這個屬性來指定要填入控制項的資料來源 RepeaterUse this property to specify the source of data to populate the Repeater control. DataSource可以是任何 System.Collections.IEnumerable 集合(例如) System.Data.DataView ,以便存取資料庫、 System.Collections.ArrayList 、或陣列,或是 IListSource 物件。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. 當您設定 DataSource 屬性時,您必須手動撰寫程式碼以系結至資料來源。When you set the DataSource property you must manually write the code to bind to the data source.

如果屬性所指定的資料來源 DataSource 包含多個資料來源,請使用 DataMember 屬性來指定要系結至控制項的特定來源。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. 例如,如果您有 System.Data.DataSet 具有多個資料表的,則必須指定要系結至控制項的資料表。For example, if you have a System.Data.DataSet with multiple tables, you must specify which table to bind to the control. 在您指定資料來源之後,請使用方法,將 DataBind 資料來源系結至控制項。After you have specified the data source, use the DataBind method to bind the data source to the control.

或者,您可以使用 DataSourceID 屬性自動系結至資料來源控制項所代表的資料來源。Alternately, you can use the DataSourceID property to automatically bind to a data source represented by a data source control. 當您設定 DataSourceID 屬性時, Repeater 控制項會自動系結至指定的資料來源控制項。When you set the DataSourceID property, the Repeater control automatically binds to the specified data source control. DataBind除非您以動態方式變更控制項的屬性,否則不需要撰寫明確呼叫方法的程式碼 RepeaterYou do not need to write code that explicitly calls the DataBind method unless you dynamically change properties of the Repeater control.

如果同時指定 DataSource 屬性和屬性的值 DataSourceID ,ASP.NET 就無法解析資料來源,而且會擲回 System.Web.HttpExceptionIf 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.

適用於

另請參閱