Repeater.DataSourceID 屬性

定義

取得或設定資料來源控制項的 ID 屬性,而 Repeater 控制項應該用這個控制項來擷取其資料來源。

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

屬性值

資料來源控制項的 ID 屬性。

例外狀況

由於下列其中一項原因,無法解析資料來源:

範例

下列程式碼範例示範如何將 屬性設定 DataSourceIDID 控制項的 SqlDataSource 屬性。 載入頁面時, Repeater 控制項會系結至控制項所 SqlDataSource 指定的資料來源,並將資料顯示給使用者。

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

備註

DataSourceID使用 屬性可存取 ID 控制項應該用來擷取其資料來源之資料來源控制項 Repeater 的 屬性。 屬性所 DataSourceID 參考的資料來源控制項可以是實作 IDataSource 介面的任何控制項。 資料來源控制項必須存在於與參考資料源的控制項相同的命名容器 Repeater 中,或在控制項的 Repeater 父控制項中。 當您指定 屬性的值 DataSourceID 時, Repeater 控制項會系結至指定的資料來源控制項。 您不需要撰寫明確呼叫 方法的程式 DataBind 代碼。

或者,您可以使用 DataSource 屬性來指定要系結至 Repeater 控制項的值來源。 資料來源必須是實 System.Collections.IEnumerable 作 介面的集合, (例如 System.Data.DataViewSystem.Collections.ArrayList 物件) 或 IListSource 介面。 當您設定 DataSource 屬性時,您必須撰寫程式碼來執行資料系結。

如果 同時 DataSource 為 和 DataSourceID 屬性指定值,ASP.NET 就無法解析資料來源,並 System.Web.HttpException 擲回例外狀況。

此屬性的值會儲存在檢視狀態中。

適用於

另請參閱