当前位置: 移动技术网 > IT编程>开发语言>.net > C#数据绑定控件中的DataSource属性浅谈

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
有的时候,你在编程进入一定阶段,进一步提升很困难的境况之下,不妨回过头来看看基础的东西,或许你会有新的受益,或许能够真正的体会到孔夫子所谓的“温故而知新”的真正内涵。常用的

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网