服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - C#数据绑定控件中的DataSource属性浅谈

C#数据绑定控件中的DataSource属性浅谈

2019-10-24 12:24C#教程网 ASP.NET教程

使用该属性指定用来填充Repeater控件的数据源。DataSource可以是任何System.Collections.IEnumerable对象, 如用于访问数据库的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、数组或IListSource对象

有的时候,你在编程进入一定阶段,进一步提升很困难的境况之下,不妨回过头来看看基础的东西,或许你会有新的受益,或许能够真正的体会到孔夫子所谓的“温故而知新”的真正内涵。
常用的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与这个类似。
程序代码

 

复制代码代码如下:

privatevoidBindData()  
{  
//通过业务逻辑,直接调用数据库中的数据  
DataTablenTable=getTable();  

Repeater1.DataSource=nTable;  
Repeater1.DataBind();  

 

HTML代码
C#数据绑定控件程序代码

复制代码代码如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate> 
<table> 
<tr> 
<thscopethscope="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
C#数据绑定控件程序代码

复制代码代码如下:

privatevoidBindData()  
{  
ArrayListlist=newArrayList();  
list.Add("Jim");  
list.Add("Tom");  
list.Add("Bluce");  
list.Add("Mary");  

Repeater1.DataSource=list;  
Repeater1.DataBind();  


HTML适当改变
程序代码

复制代码代码如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate><table><tr><thscopethscope="col">姓名<th><tr><HeaderTemplate> 
<ItemTemplate><tr><td><%#Container.DataItem%><td><tr><ItemTemplate> 
<FooterTemplate><table><FooterTemplate> 
<asp:Repeater> 


<3>绑定Dictionary、HashTable
Dictionary
C#数据绑定控件程序代码

复制代码代码如下:

privatevoidBindData()  
{  
Dictionary<string,int>dic=newDictionary<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:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate><table><tr><thscopethscope="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。简单明了。
一个简单的用户类,包含两个公共属性。
程序代码

复制代码代码如下:


usingSystem;  
usingSystem.Data;  

///

///SummarydescriptionforUser  
///

publicclassUser  
{  
privatestring_Name;  
publicstringName  
{  
get{return_Name;}  
set{_Name=value;}  
}  
privateint_Age;  
publicintAge  
{  
get{return_Age;}  
set{_Age=value;}  
}  
publicUser()  
{  
//  
//TODO:Addconstructorlogichere  
//  
}  
publicUser(stringname,intage)  
{  
_Name=name;  
_Age=age;  
}  
}

 

 


绑定对象集合:
IList
程序代码

复制代码代码如下:

privatevoidBindData()  
{  
Useruser1=newUser("Jim",21);  
Useruser2=newUser("Tom",23);  
Useruser3=newUser("Bluce",33);  
Useruser4=newUser("Mary",18);  

IList<User>list=newList<User>();  
list.Add(user1);  
list.Add(user2);  
list.Add(user3);  
list.Add(user4);  

Repeater1.DataSource=list;  
Repeater1.DataBind();  


对应的Repeater绑定对象的公共属性:
C#数据绑定控件程序代码

复制代码代码如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate> 
<table> 
<tr> 
<thscopethscope="col"> 
姓名th> 
<th> 
年龄<th> 
<tr> 
<HeaderTemplate> 
<ItemTemplate> 
<tr> 
<td> 
<%#Eval("Name")%> 
<td> 
<td> 
<%#Eval("Age")%> 
<td> 
<tr> 
<ItemTemplate> 
<FooterTemplate> 
<table><FooterTemplate> 
<asp:Repeater> 
 

延伸 · 阅读

精彩推荐