Step 7: Learn ASP. NET data binding step by step

Source: Internet
Author: User
Tags reflector

It is also strange to say that a Web project has recently been developed and paging is required, so the study of data binding has become very important. Next, let's take a look at the blog's article!

1. Data Binding expression

① Expression

<% # Data binding expression %>

② Write code on the page

<% = C # code %> // This expression is used to call background variables or methods.

 

2. One-way binding

A one-way read-only data value is provided. data can only be read from the data source and cannot be modified!

Syntax:

Eval ("Column name|Attribute name and so on")

Principle: The reflection mechanism is used to implement binding computing. Writing "Eval" will call the static method Eval in DataBinder at the underlying layer!

Recommended:DataBinder. Eval ()Bind data in this way!

  

The above are his three overload methods!

The first parameter is always →Container. DataItem

The second parameter is → the data you want to bind, such as the column name and attribute name.

The third parameter is → string formatting

For more information, see:In-depth ASP. NET data binding (I)This is a good article on one-way data binding!

 

3. Bidirectional binding

Two-way binding: you can read the value of the data source and modify the value of the data source for data updates. It is mainly used for controls that support the editing function, such as GridView and DataList!

Syntax:

Bind ("column name | attribute name, etc ")

Recommended articles:In-depth ASP. NET data binding (medium)-data two-way binding mechanism


4. What types of data binding expressions can be used?

① It can be a variable.

Example: <asp: Label ID = "Label1" runat = "server" Text = "<% = variable name %>"> </asp: Label>

Note that if it is a variable, the reverse question modifier of the variable cannot be private. It must be "public" or "protected!

Note:The variable must use "=" to be effective!


② It can be the property value of the server control

Example: <asp: Label ID = "Label1" runat = "server" Text = "<% # TextBox2.Text %>"> </asp: Label>


③ It can be an array or set.

This type is mainly for controls such as "DropDownList" and "ListBox!

There is an array: public string [] str = {"22", "333", "4444444", "55555555555 "};

Then you can bind it to the front end: <asp: DropDownList ID = "DropDownList1" runat = "server" DataSource = "<% # str %>"> </asp: DropDownList>

Note:You must call the "DataBind" method of the control in the background, otherwise it will not work!


④ It can be an expression

You can use several types of data and connect them to the desired data!

For example, to display the full name of a user in a Label, we can write <% # (Person. FirstName + "" + Person. LastName) %> in this way.


⑤ It Can Be A Method

At first, I thought that the above binding method was used, but I tried it many times and it didn't work. Finally, I used another method → <%= MyReturn ()%>, An equal sign is added, instead of "#"!


⑥ DataTable, DataSet, AND OTHER TYPES

Data Binding between Eval and DataBinder. Eval!

Such a data binding expression can only be used in controls such as Repeater, GridView, and DataList!

Recommended articles:Introduction to the data binding expression in. NET (1)

 

5. Evolution of data binding expressions

① First: The most common

"Eval" is the most commonly used in controls such as GridView, Repeater, and DataList!


② Second: Father of the first method

This method is also common:

<% # DataBinder. Eval (Container. DataItem, "max_lvl", "{0: c}") %>

Note:The only thing to note is the parameter selection!

The first method is to simplify the second method, and the bottom layer will still call the second method!


③ Category 3: Best Performance

If the Data type is able or DataSet, you must use the namespace → <% @ Import namespace = "System. Data" %>

<% # (DataRowView) Container. DataItem) ["age"] %>

You can also write as follows:

<% # (Container. DataItem as DataRowView) ["ProductName"]. ToString () %>

Syntax:

(DataRowView) (Container. DataItem) ["column name"]/[column Index]

General Syntax: (Type) (Container. DataItem) + the value you want to obtain

Note:Type is your data Type. It can be a set, a able, and so on!

Evolution History: The third type → the second type → the first type. Therefore, many people are still using Eval to bind data. Anyone who has learned how to bind Data knows that the third method is the best!

Recommended articles:Introduction to the data binding expression in. NET (1)

 

6. Viewing the Assembly on the dynamic compilation page helps us understand the underlying principles of data binding.

To be honest, I don't know how to express it in professional terms. My understanding is that this Assembly is an intermediate language based on C # code and HTML!

Dynamic Assembly viewing address:

C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ Temporary ASP. NET Files

Find the Assembly and use Reflector to view the Assembly. If you look at it carefully, I believe you should have a lot of gains!

Recommended articles ::In-depth ASP. NET data binding (on ),Step by step to familiarize you with page compilation code!

 

7. Custom Data Binding format

This idea comes from the innovative idea of a bloggers! Refer:How to build strong ASP. NET data binding in 30 seconds!

Dudu Daniel's Ultimate Edition:Strong ASP. NET data binding Ultimate Edition,And enhanced version:Strong ASP. NET data binding Ultimate Edition 2nd!

The custom code is as follows:

1 public static class DataBindHelper
2 {
3 public static TResult Eval <TEntity, TResult> (this Page, Func <TEntity, TResult> func)
4 {
5 var item = page. GetDataItem (); // This is the core code
6 return func (TEntity) item );
7}
8}

DEMO code of foreground call:

1      <asp:Repeater ID="Repeater1" runat="server">
2 <ItemTemplate>
3 <%# DataBindHelper.Eval<People,string>(this,p=>p.Name)%>
4 </ItemTemplate>
5 </asp:Repeater>

You can bind data in a more user-friendly manner through custom data binding formats!

Note:It is best to experiment on VS2010. Because I have the same code as 2010 and 2008, error 2008 will be reported during runtime, "invalid expression"> ". If you know, please let me know!
    

8. Talking about the underlying mechanism of Data Binding

Remember the phrase "page. the GetDataItem () code is the core of Data Binding. It means to get the object at the top of the binding context stack!

Some people say they have understood this, and the data binding technology is 80%-90%. So next we will focus on it!

To put it bluntly, we read data from the heap, but Microsoft encapsulated it so badly that we only saw the gorgeous appearance!

Recommended article: thoroughly analyzes the performance of various data binding expressions →Introduction to the data binding expression in. NET (2)


9. Summary

Flexible running of data binding technology can speed up development, but do not abuse it. It is also important to run suitable data binding on appropriate occasions and select the appropriate data binding format!

1 <% # DataBindHelper. Eval <People, string> (this, p => p. Name) %> // define the data binding format
2 <% # DataBinder. GetDataItem ("Name") %>
3 <% # (People) GetDataItem (). Name %>
4 <% # (People) Container. DataItem). Name %>
5 <% # Eval ("Name") %>
6 <% # DataBinder. Eval (Container. DataItem, "Name", "") %>
7 <% # (System. Data. DataRowView) GetDataItem () [0] %>
8 <% # (System. Data. DataRowView) Container. DataItem) [0] %>

In fact, if you carefully find out and use Reflector to view the source code, you will know that"GetDataItemThis method is actually the most important. You can use it to obtain the object at the top of the binding context stack to bind it!


Finally, if you follow the steps in this article to learn more about the articles, I believe you will have a deep understanding of data binding!

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.