Sometimes, you in the programming to enter a certain stage, further improve the very difficult situation, may wish to look back at the foundation of things, perhaps you will have a new benefit, may be able to really appreciate the so-called "Wen Zhi xin" the true connotation.
Common C # Data-bound controls are: Repeater, DataList, GridView, DetailsView, and so on, here I take Repeater to simply explain the problem.
Use 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 System.Data.DataView, System.Collections.ArrayList, System.Collections.Hashtable, arrays, or IListSource objects used to access the database.
Common data Sources:
A DataTable
A DataView
a dataset
Any component that implements the IListSource interface
Any component that implements the IList interface
Attention:
To bind to a strongly typed array of objects, the object type must contain public properties.
The following is a few simple examples to introduce the specific application of datasource.
<1> binding a DataTable is usually to take data out of the database and then bind directly, and the logic of the database operation is no longer available. It must have been very familiar to everyone. Binding DataView is similar to this.
Program code
Privatevoidbinddata () { ///Through the business logic, directly call the data in the database datatablentable=gettable (); repeater1.datasource=ntable; Repeater1.databind (); }
HTML code
C # Data-bound control program code
<asp:repeateridasp:repeaterid= "Repeater1" runat= "Server" > <HeaderTemplate> <table> <tr> <thscopethscope= "col" > 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> binds array, ArrayList, List, one-dimensional arrays, and so on, which store simple data.
ArrayList
C # Data-bound control program code
Privatevoidbinddata () { arraylistlist=newarraylist (); List. ADD ("Jim"); List. ADD ("Tom"); List. ADD ("Bluce"); List. ADD ("Mary"); Repeater1.datasource=list; Repeater1.databind (); }
HTML changes appropriately
Program code
<asp:repeateridasp:repeaterid= "Repeater1" runat= "Server" >
<3> Binding dictionary, HashTable
Dictionary
C # Data-bound control program code
Privatevoidbinddata () { dictionary<string,int>dic=newdictionary<string,int> (); Dic. ADD ("Jim", +); Dic. ADD ("Tom", +); Dic. ADD ("Bluce"); Dic. ADD ("Mary",); Repeater1.datasource=dic; Repeater1.databind (); }
HTML code
Program code
<asp:repeateridasp:repeaterid= "Repeater1" runat= "Server" >
<4> Binding Object collection, IList, and so on. This is very useful, in our data query, often take out data from the database, in order to facilitate operation, we need to encapsulate objects, but sometimes need to display these objects as a list, a solution: the object is converted to a DataTable, the other is to call the database directly. Both of these options are not ideal. And here directly binds the object collection directly to the data display control, and shows me a way out. In fact, the PetShop4.0 is the use of this, binding ICollection or IList. Simple and clear.
A simple user class that contains two public properties.
Program code
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; } }
Binding Object collection:
IList
Program code
Privatevoidbinddata () { useruser1=newuser ("Jim", +); Useruser2=newuser ("Tom", at a); 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:repeateridasp:repeaterid= "Repeater1" runat= "Server" > <HeaderTemplate> <table> <tr> <thscopethscope= "col" > Name th> <th> age <th> <tr> <HeaderTemplate> <ItemTemplate> <tr> <td> <% #Eval ("Name")%> <td> <td> <% #Eval ("Age")%> <td> <tr> < itemtemplate> <FooterTemplate> <table><FooterTemplate> <asp:Repeater>
These are the DataSource properties in C # data-bound controls, and more about topic.alibabacloud.com (www.php.cn)!