Data Binding and analysis of several methods and usage of Container. DataItem

Source: Internet
Author: User

Flexible Use of Data Binding
Bind to simple attributes: <% # UserName %>
Bind to collection: <asp: ListBox id = "ListBox1" datasource = '<% # myArray %> 'runat = "server">
Binding to expression: <% #(class1.property1. ToString () + "," + class1.property2. ToString () %>
Return Value bound to the method: <% # GetSafestring (str) %>
Bind to Hashtable: <% # (DictionaryEntry) Container. DataItem). Key %>
Bind to ArrayList: <% # Container. DataItem %>

If an object is put in the array, you may need to convert the object before binding it, for example:
<% # (Object Type) Container. DataItem). Attribute %>

Bind to DataView, able, DataSet:
<% # (DataRowView) Container. DataItem) ["field name"] %> or
<% # (DataRowView) Container. DataItem). Rows [0] ["field name"] %>
To Format:
<% # String. Format ("Format", (DataRowView) Container. DataItem) ["field name"]) %>
<% # DataBinder. Eval (Container. DataItem, "field name", "format") %>

Bind to DataReader:
<% # (IDataReader) Container. DataItem). Field name %>

Of course, the Eval method of the DataBinder class is generally used for convenience. However, it is less efficient to bind a large amount of data at the same time.

This program is often used when binding data: <% # DataBinder. eval (Container. dataItem, "xxxx") %> or <% # DataBinder. eval (Container, "DataItem. xxxx ") %>
Today I learned another method, and Microsoft also said that this method is more efficient than the above two methods.

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

Very useful, so that you can do a lot of things on the front-end page.

Remember to import the namespace System. Data on the front-end page. Otherwise, an error message is generated.

<% @ Import namespace = "System. Data" %>

This usage is actually the same as <% # (DictionaryEntry) Container. DataItem). Key %>.

When binding to DataSet or able:

<% # (System. Data. DataRowView) Container. DataItem) ["field name"] %>
<% # (System. Data. DataRowView) Container. DataItem) [Index] %>


When bound to DataReader:
<% # (System. Data. Common. DbDataRecord) Container. DataItem) [Index] %>
<% # (System. Data. Common. DbDataRecord) Container. DataItem) ["field name"] %>

The key is the Container, which is mysterious. Its namespace is System. ComponentModel. I need to further understand it.

Beginner. NET, now reading the DataGrid Control, when ItemTemplate displays data,
What is the difference between DataBinder. Eval (Container. DataItem, "Name") and Container. DataItem ("Name?


DataBinder is System. A static class in the Web, which provides the Eval method to simplify writing data binding expressions. However, it uses Reflection and other overhead methods to achieve ease of use, therefore, its performance is not the best. The Container is not a static object or method at all. It is ASP. NET page compiler local variables declared in the Data Binding event handler, its type is the data container type of controls that can be bound to data (for example, the data binding container inside the Repeater is called RepeaterItem). In these container classes, DataItem attributes are basically available, therefore, you can write Container. dataItem, which returns the data item from the data source you are bound. If your data source is DataTable, the Data Type of this data item is actually DataRowView.


From jarin's column

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.