C # Introduction to DataSource attributes in the data binding control

Source: Internet
Author: User

Sometimes, when you enter a certain stage of programming and further improve the system, you may wish to look back at the basic things, and perhaps you will have a new benefit, it may be able to truly understand the true meaning of Confucius's so-called "temperature and knowledge of the new.
Common C # data binding controls include Repeater, DataList, GridView, and DetailsView. Here I will use Repeater to briefly describe the problem.
This attribute is used to specify the data source used to fill the Repeater control. DataSource can be any System. Collections. IEnumerable object,
For example, the System. Data. DataView, System. Collections. ArrayList, System. Collections. Hashtable, array, or IListSource object used to access the database.
Common data sources:
A DataTable
One DataView
One DataSet
Any component that implements the IListSource Interface
Any component that implements the IList Interface
Note:
To bind to a strongly typed array of an object, the object type must contain public attributes.
The following describes the specific application of DataSource through several simple examples.
<1> to bind a able, data is usually taken out of the database and then bound directly. The logic of the specific database operation is not provided. I think everyone is very familiar with it. Binding DataView is similar to this.
Program code

Copy codeThe Code is as follows:
PrivatevoidBindData ()
{
// Directly call data in the database through business logic
DataTablenTable = getTable ();

Repeater1.DataSource = nTable;
Repeater1.DataBind ();
}

HTML code
C # data binding control program code
Copy codeThe Code is as follows:
<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> bind an Array, ArrayList, List, or one-dimensional Array to store simple data.
ArrayList
C # data binding control program code
Copy codeThe Code is as follows:
PrivatevoidBindData ()
{
ArrayListlist = newArrayList ();
List. Add ("Jim ");
List. Add ("Tom ");
List. Add ("Bluce ");
List. Add ("Mary ");

Repeater1.DataSource = list;
Repeater1.DataBind ();
}

HTML change as appropriate
Program code
Copy codeThe Code is as follows:
<Asp: RepeaterIDasp: RepeaterID = "Repeater1" runat = "server">
<HeaderTemplate> <table> <tr> <thscopethscope = "col"> name <th> <tr> <HeaderTemplate>
<ItemTemplate> <tr> <td> <% # Container. DataItem %> <td> <tr> <ItemTemplate>
<FooterTemplate> <table> <FooterTemplate>
<Asp: Repeater>

<3> bind Dictionary and HashTable
Dictionary
C # data binding control program code
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
<Asp: RepeaterIDasp: RepeaterID = "Repeater1" runat = "server">
<HeaderTemplate> <table> <tr> <thscopethscope = "col"> name <th> age <th> <tr> <HeaderTemplate>
<ItemTemplate> <tr> <td> <% # Eval ("Key") %> td> <% # Eval ("value ") %> <td> <tr> <ItemTemplate>
<FooterTemplate> <table> <FooterTemplate>
<Asp: Repeater>

<4> bind an object set, such as IList. This is very useful. When we query data, we often retrieve data from the database. To facilitate operations, we need to encapsulate the data into objects, but sometimes these objects need to be displayed in the form of a list. One solution is to convert an object to a able, and the other is to directly call the database. These two solutions are not ideal. In this case, the object set is directly bound to the data display control, indicating a way out for me. In fact, this is used in PetShop4.0 to bind ICollection or IList. Simple and clear.
A simple user class contains two common attributes.
Program code
Copy codeThe Code is as follows:
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;
}
}


Bound object set:
IList
Program code
Copy codeThe Code is as follows:
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 ();
}

Public attributes of the corresponding Repeater bound object:
C # data binding control program code
Copy codeThe Code is as follows:
<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.