常用的C#数据绑定控件有:Repeater、DataList、GridView、DetailsView等,在这里我拿Repeater来简单说明问题。
使用该属性指定用来填充Repeater控件的数据源。DataSource可以是任何System.Collections.IEnumerable对象,
如用于访问数据库的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、数组或IListSource对象。
常用的数据源:
一个DataTable
一个DataView
一个DataSet
任何实现IListSource接口的组件
任何实现IList接口的组件
注意:
若要绑定到对象的强类型数组,该对象类型必须包含公共属性。
下面通过几个简单的实例来介绍DataSource的具体应用。
<1>绑定DataTable,一般都是从数据库取出数据,然后直接进行绑定,具体的数据库操作的逻辑不再提供。想必大家都已经非常熟悉。绑定DataView与这个类似。
- { //通过业务逻辑,直接调用数据库中的数据
- DataTable nTable=getTable();
- Repeater1.DataSource=nTable;
- Repeater1.DataBind();
- }
HTML代码
- <HeaderTemplate>
- <table> <tr> <th> 姓名</th> <th> 年龄</th> </tr>
- </HeaderTemplate>
- <ItemTemplate> <tr> <td> <%#Eval("Key")%> </td> <td> <%#Eval("value")%> </td> </tr>
- </ItemTemplate>
- <FooterTemplate> </table></FooterTemplate> </asp:repeater>
<asp:repe