BaseDataList.DataSource 属性

定义

获取或设置源,该源包含用于填充控件中的项的值列表。Gets or sets the source containing a list of values used to populate the items within the control.

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)]
[System.Web.UI.Themeable(false)]
public virtual object DataSource { get; set; }
member this.DataSource : obj with get, set
Public Overridable Property DataSource As Object

属性值

一个 IEnumerableIListSource,包含用于为此控件提供数据的值集合。An IEnumerable or IListSource that contains a collection of values used to supply data to this control. 默认值是 nullThe default value is null.

属性

异常

无法解析数据源,因为对 DataSource 属性和 DataSourceID 属性指定了同一个值。The data source cannot be resolved because a value is specified for both the DataSource property and the DataSourceID property.

数据源的类型无效。The data source is of an invalid type. 数据源必须为 null,或者实现 IEnumerableIListSource 接口。The data source must be null or implement either the IEnumerable or the IListSource interface.

示例

下面的代码示例演示如何使用 DataSource 属性指定要绑定到 DataGrid 控件的数据源。The following code example demonstrates how to use the DataSource property to specify the data source to bind to a DataGrid control.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script language="C#" runat="server">
 
      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         if (!IsPostBack) 
         {
            // Load this data only once.
            ItemsGrid.DataSource= CreateDataSource();
            ItemsGrid.DataBind();
         }
      }
 
   </script>
 
<head runat="server">
    <title>DataGrid Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle> 
 
      </asp:DataGrid>
 
   </form>
 
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script language="VB" runat="server">
     Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
        
        Dim i As Integer
        For i = 0 To 8
            dr = dt.NewRow()
            
            dr(0) = i
            dr(1) = "Item " + i.ToString()
            dr(2) = 1.23 *(i + 1)
            
            dt.Rows.Add(dr)
        Next i
        
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource


    Sub Page_Load(sender As Object, e As EventArgs)
        
        If Not IsPostBack Then
            ' Load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
        End If
    End Sub 'Page_Load
 
  </script>
 
<head runat="server">
    <title>DataGrid Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle> 
 
      </asp:DataGrid>
 
   </form>
 
</body>
</html>

注解

使用 DataSource 属性指定要绑定到数据列表控件的值的源。Use the DataSource property to specify the source of values to bind to a data listing control. 数据源必须是实现 System.Collections.IEnumerable 接口(如 System.Data.DataViewSystem.Collections.ArrayListSystem.Collections.Generic.List<T>)的集合,或者是绑定到从 IListSource 类派生的控件的 BaseDataList 接口。A data source must be a collection that implements either the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Generic.List<T>) or the IListSource interface to bind to a control derived from the BaseDataList class. 设置 DataSource 属性时,必须手动编写代码以执行数据绑定。When you set the DataSource property, you must manually write the code to perform data binding.

如果 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 object 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 属性时,数据列表控件将自动绑定到指定的数据源控件。When you set the DataSourceID property, the data listing control automatically binds to the specified data source control. 无需编写显式调用 DataBind 方法的代码。You do not need to write code that explicitly calls the DataBind method.

如果为 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 an System.Web.HttpException exception is thrown.

无法通过主题或样式表主题设置此属性。This property cannot be set by themes or style sheet themes. 有关详细信息,请参阅 ThemeableAttributeASP.NET 主题和外观For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

适用于

另请参阅