DataSource属性
来源:互联网 发布:java 开源连接池 编辑:程序博客网 时间:2024/05/18 20:36
数据绑定控件,都有DataSource属性,用于指定数据源。通常情况下,也是比较常用的就是数据源是数据表(DataTable),其实DataSource的范围很广,我们可以在深入研究的同时,改善我们的程序。
有的时候,你在编程进入一定阶段,进一步提升很困难的境况之下,不妨回过头来看看基础的东西,或许你会有新的受益,或许能够真正的体会到孔夫子所谓的“温故而知新”的真正内涵。
常用的数据绑定控件有: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与这个类似。
private void BindData() { //通过业务逻辑,直接调用数据库中的数据 DataTable nTable = getTable(); Repeater1.DataSource = nTable; Repeater1.DataBind(); }
Html代码
Repeater <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr> <th scope="col"> 姓名</th> <th> 年龄</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%#Eval("Key") %> </td> <td> <%#Eval("value") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table></FooterTemplate> </asp:Repeater>
<2>绑定Array、ArrayList、List、一维数组之类,里面存储简单的数据。
ArrayList
private void BindData() { ArrayList list = new ArrayList(); list.Add("Jim"); list.Add("Tom"); list.Add("Bluce"); list.Add("Mary"); Repeater1.DataSource = list; Repeater1.DataBind(); }
Html适当改变
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate><table><tr><th scope="col">姓名</th></tr></HeaderTemplate> <ItemTemplate><tr><td><%#Container.DataItem %></td></tr></ItemTemplate> <FooterTemplate></table></FooterTemplate></asp:Repeater>
<3>绑定Dictionary、HashTable
Dictionary
private void BindData() { Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("Jim", 21); dic.Add("Tom", 26); dic.Add("Bluce", 33); dic.Add("Mary", 18); Repeater1.DataSource = dic; Repeater1.DataBind(); }
Html代码
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate><table><tr><th scope="col">姓名</th><th>年龄</th></tr></HeaderTemplate> <ItemTemplate><tr><td><%#Eval("Key") %></td><td><%#Eval("value") %></td></tr></ItemTemplate> <FooterTemplate></table></FooterTemplate></asp:Repeater>
<4>绑定对象集合,IList等。这个很是有用,在我们进行数据查询的时候,经常从数据库取出数据,为了方便操作,需要封装成对象,但是有的时候需要将这些对象以列表的形式显示出来,一种解决方案:对象转换为DataTable,另一种就是直接调用数据库。这两种方案,并不是很理想。而这里直接将对象集合直接绑定到数据显示控件,给我指明一条出路。其实,在PetShop4.0就是利用这一点,绑定ICollection或者IList。简单明了。
一个简单的用户类,包含两个公共属性。User
using System;using System.Data;/// <summary>/// Summary description for User/// </summary>public class User{ private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private int _Age; public int Age { get { return _Age; } set { _Age = value; } } public User() { // // TODO: Add constructor logic here // } public User(string name,int age) { _Name = name; _Age = age; }}
绑定对象集合:
IList
private void BindData() { User user1 = new User("Jim", 21); User user2 = new User("Tom",23); User user3 = new User("Bluce",33); User user4 = new User("Mary",18); IList<User> list = new List<User>(); list.Add(user1); list.Add(user2); list.Add(user3); list.Add(user4); Repeater1.DataSource = list; Repeater1.DataBind(); }
对应的Repeater绑定对象的公共属性:
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr> <th scope="col"> 姓名</th> <th> 年龄</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%#Eval("Name") %> </td> <td> <%#Eval("Age") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table></FooterTemplate> </asp:Repeater>
- DataSource属性
- SpringBoot配置属性之DataSource
- SpringBoot配置属性之DataSource
- SpringBoot配置属性之DataSource
- SpringBoot配置属性之DataSource
- DataGrid.DataSource 属性的VB.NET例子
- ComboBox的DataSource设置即相关属性
- C#数据绑定控件中的DataSource属性
- datalist控件的DataSourceID与DataSource属性
- GridView的DataSourceID vs. DataSource属性
- DevExpress- CheckedListBoxControl DataSource属性存在BUG
- 三十五、SpringBoot配置属性之DataSource
- SpringBoot配置属性之DataSource(三)
- DataSource
- DataSource
- datasource
- dataSource
- dataSource
- 传奇私服务喊话宣传工具[图]
- VBA实用开发指南(3)
- JS将浮点型数字转化为货币型格式
- VBA实用开发指南(4)
- 使用设计模式改善程序结构(一)
- DataSource属性
- 标签库的深入研究
- 开始
- sql server 存储过程 实现模糊查询
- 大家看看我用C语言开发的网站
- c#写的串口通讯
- DVB标准组成
- C#异步数据接收串口操作类
- 使用设计模式改善程序结构(二)