Repeater.DataSource 属性
定义
获取或设置为填充列表提供数据的数据源。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
属性值
IEnumerable 或 IListSource 对象,包含用于为此控件提供数据的值的集合。An IEnumerable or IListSource object that contains a collection of values used to supply data to this control. 默认值是 null。The 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>
注解
使用此属性指定填充控件的数据源 Repeater 。Use 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 除非动态更改控件的属性 Repeater 。You 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.HttpException 引发。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.