Home  >  Article  >  Backend Development  >  A brief discussion on the DataSource property in C# data binding controls

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

黄舟
黄舟Original
2017-01-19 11:15:342130browse

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.
f35d6e602fd7d0f0edfa6f7d103c1b57Binding 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();    
}

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>

2cc198a1d5eb0d3eb508d858c9f5cbdbBind 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();    
}

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>

5bdf4c78156c7953567bb5a0aef2fc53Bind 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();    
}


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>

23889872c2e8594e0f446a471a78ec4cBind 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;  
}  
}

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

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>

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)!


Statement:
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