A brief discussion on the DataSource property in C# data binding controls

黄舟
Release: 2017-01-19 11:15:34
Original
2079 people have browsed it

Sometimes, when you enter a certain stage of programming and it is very difficult to further improve, you might as well go back and look at the basic things. Maybe you will gain new benefits, and maybe you can truly understand what Confucius said. The true meaning of “reviewing the past and learning the new”.
Commonly used C# data binding controls include: Repeater, DataList, GridView, DetailsView, etc. Here I use Repeater to briefly explain the problem.
Use this property to specify the data source used to fill the Repeater control. DataSource can be any System.Collections.IEnumerable object,
such as System.Data.DataView, System.Collections.ArrayList, System.Collections.Hashtable, array or IListSource object used to access the database.
Commonly used data sources:
A DataTable
A DataView
A DataSet
Any component that implements the IListSource interface
Any component that implements the IList interface
Note:
If To bind to a strongly-typed array of objects, the object type must contain public properties.
The specific applications of DataSource are introduced below through several simple examples.
<1>Binding a DataTable generally involves retrieving data from the database and then binding it directly. The specific database operation logic is no longer provided. Everyone must be very familiar with it. Binding a DataView is similar to this.
Program code

privatevoidBindData()    
{    
//通过业务逻辑,直接调用数据库中的数据    
DataTablenTable=getTable();    
  
Repeater1.DataSource=nTable;    
Repeater1.DataBind();    
}
Copy after login

HTML code
C#Data binding control program code

<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>
Copy after login

<2>Bind Array, ArrayList, List, one-dimensional array, etc., inside Store simple data.
ArrayList
C#Data binding control program code

privatevoidBindData()    
{    
ArrayListlist=newArrayList();    
list.Add("Jim");    
list.Add("Tom");    
list.Add("Bluce");    
list.Add("Mary");    
  
Repeater1.DataSource=list;    
Repeater1.DataBind();    
}
Copy after login

HTML appropriate changes
Program code

<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>
Copy after login

<3>Bind Dictionary, HashTable
Dictionary
C#Data binding control program code

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();    
}
Copy after login


HTML code
Program code

<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>
Copy after login

<4>Bind object collection, IList, etc. This is very useful. When we perform data query, we often retrieve data from the database. In order to facilitate operation, it needs to be encapsulated into objects. However, sometimes these objects need to be displayed in the form of a list. One solution: object conversion For DataTable, the other is to call the database directly. These two options are not ideal. And here, the object collection is directly bound to the data display control, pointing out a way out for me. In fact, in PetShop4.0, this is used to bind ICollection or IList. easy to understand.
A simple user class containing 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;  
}  
}
Copy after login

Binding object collection:
IList
Program code

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();    
}
Copy after login

The public properties of the corresponding Repeater binding object:
C#Data binding Control program code

<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>
Copy after login

The above is the content of the DataSource property in the C# data binding control. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!