Commonly used C # data-bound controls are: Repeater, DataList, GridView, DetailsView, and so on, here I take Repeater to simply explain the problem. The
uses this property to specify the data source that is used to populate the Repeater control. DataSource can be any System.Collections.IEnumerable object,
such as the System.Data.DataView used to access the database, System.Collections.ArrayList, System.Collections.Hashtable, array, or IListSource object.
Common data sources:
A DataTable
a DataView
a dataset
Any component that implements the IListSource interface
Any component that implements the IList interface
Note:
to bind to an object's strongly typed array, the object type must contain public properties.
The following are a few simple examples to illustrate the specific application of datasource.
<1> bind a DataTable, typically fetching data from a database and then binding directly, and the logic of the database operation is no longer available. Presumably everyone is already very familiar with it. The binding DataView is similar to this one. Private void binddata () { // Through business logic, directly call data in the database datatable ntable=gettable (); repeater1.datasource=ntable; repeater1.databind (); }
HTML code <asp:repeater id= "Repeater1" runat= "Server" > < headertemplate> <table> <tr> <th> name </th> < th> Age </th> </tr> </HeaderTemplate> < itemtemplate> <tr> <td> <% #Eval ("Key")%> </td> <td> <% #Eval ("value")%> </td> </tr> </ItemTemplate> <FooterTemplate> </table></FooterTemplate> </asp:repeater>
<asp:repeater id= "Repeater1" runat= "Server" >
<HeaderTemplate>
<table> <tr> <th > Name </th> <th> age </th> </tr>
</HeaderTemplate> <ItemTemplate>
<tr > <td> <% #Eval ("Key")%> </td> <td> <% #Eval ("value")%> </td> </tr>
</ItemTemplate>
<FooterTemplate> </table></FooterTemplate> </asp:repeater>
<2> binding Array, ArrayList, List, one-dimensional array, and so on, which store simple data. arraylistc# data-bound control program code private void binddata () {arraylistlist=newarraylist (); List. ADD ("Jim"); List. ADD ("Tom"); List. ADD ("Bluce"); List. ADD ("Mary"); Repeater1.datasource=list; Repeater1.databind (); }
HTML appropriate change
1.<asp:repeater id= "Repeater1" runat= "Server" >
2.
3.<itemtemplate><tr><td><% #Container. dataitem%></td></tr></itemtemplate >
4.<footertemplate></table></footertemplate>
5.</asp:repeater>
<3> binding Dictionary, hashtabledictionaryc# data-bound control program code private void binddata () {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 Code Program code
<asp:repeater repeaterid= "Repeater1" runat= "Server" >
<4> Binding Objects collection, IList, etc. This is very useful, when we do data query, often from the database to retrieve data,
For ease of operation, you need to encapsulate objects, but sometimes you need to display them as a list,
A solution: Objects are converted to DataTable,
The other is to call the database directly.
Both of these options are not ideal.
This directly binds the collection of objects directly to the data display control, which gives me a way out.
In fact, in PetShop4.0 is to use this, binding ICollection or IList.
Simple and clear. A simple user class that contains two public properties. program code using System; Using System.Data; Summarydescriptionforuser/// PublicclassUser {PrivateString_name; PublicString Name {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; } }
Bound object collection:
IList
Program code private void binddata () { useruser1=newuser ("Jim"); Useruser2 =newuser ("Tom",); useruser3=newuser ("Bluce"); useruser4=newuser ("Mary", ; ilist<user>list=newlist<user> (); list. Add (user1); list. ADD (user2); list. ADD (user3); list. ADD (user4); repeater1.datasource=list; repeater1.databind ( ); }
The public properties of the corresponding Repeater bound object:
C # Data-bound control program code <asp:repeater id= "Repeater1" runat= "Server" > <HeaderTemplate> <table> <tr> <t Hscopethscope= "col" > Name </th> <th> age </th> </tr> </HeaderTemplate> <itemtemplate& Gt <tr> <td> <% #Eval ("Name")%> </td> <td> <% #Eval ("Age")%> </td> </tr> </ItemTemplate> <FooterTemplate> </table></FooterTemplate>
</asp:Repeater>