Container.DataItem several ways.
This procedure is often used when binding data: <%# DataBinder.Eval (Container.DataItem, "xxxx")%> or <%# DataBinder.Eval (Container, " Dataitem.xxxx ")%>
Today, and Microsoft says this approach is more efficient than both.
<%# ((DataRowView) container.dataitem) ["XXXX"]%>
Very useful, so you can do a lot of things on the front page.
And remember to use it. You must import the namespace System.Data on the foreground page, or you will generate an error message.
<%@ Import namespace= "System.Data"%>
This usage actually and
<%# ((Userinfomodel) Container.DataItem). Key%>
<%# ((DictionaryEntry) Container.DataItem). Key%>
is a truth.
The key is container this thing, it is more mysterious. Its namespace is System.ComponentModel. I need to be more understanding about it.
Source: http://www.cnblogs.com/huabei504/archive/2005/03/01/110618.html
Asp. net-Introduction to Container DATAITEM and eval method (reprint)
Container is a data container that represents a row in a collection class or DataView, and container. DataItem represents the data for the row; All container are stored in a stack stack, automatically pressing each container into the stack, the last line recording the advanced stack, and finally the first line; (or the top of the stack is the first container of the collection Class)
Each row of the GridView is a container (container) object of type GridViewRow. GridViewRow has a property called DataItem, which is the underlying data object to which the GridViewRow object is bound, in general it is a row (DataRow) of the relational database data source bound to the GridView.
In the above example, Container.DataItem can be converted to Teminformation because GridViewRow (that is, the GridView row) has been bound to a Iteminformation object through the original data binding.
In summary, container is a row of the GridView, and Container.DataItem is the data that this line binds to.
Each DataItem represents an object within the collection class, or a row of records within a DataView (DataRowView);
In addition, the eval (object,string) method uses a reflection mechanism to look for a string within an object, so there is a slight loss of performance.
Eval (container. DataItem, "name"): Represents the value of the name found within the current DataItem object
When using Repeater, Container.DataItem is used. Actually DataItem is an object, and this object is an element in the DataSource of repeater.
If DATASOURC is a DataTable, then this DataItem is a DataRow.
If DataSource is list<article> then this dataitem is article. Here article is a custom class. You can do the type conversion first and then call the article method. is more efficient than using databinder.eval because databinder.eval need to use reflection.
Examples of using data binding:
Bind to Simple property: <% #UserName%>
Bind to Collection: <asp:listbox id= "ListBox1" datasource= ' <%# myarray%> ' runat= "Server" >
Binding to an expression: <%# (class1.property1.ToString () + "," + class1.property2.ToString ())%>
Bind to method return value: <%# getsafestring (str)%>
Bind to hashtable:<%# ((DictionaryEntry) Container.DataItem). Key%>
Bind to arraylist:<% #Container. DataItem%>
If an object is placed in an array, it may need to be converted and then bound as:
<%# ((object type) Container.DataItem). Property%>
Bind to Dataview,datatable,dataset:
<%# ((DataRowView) Container.DataItem) ["Field name"]%> or
<%# ((DataRowView) Container.DataItem). rows[0]["Field name"]%>
To format it:
<% #string. Format ("Formatting", ((DataRowView) Container.DataItem) ["Field name]")%>
<% #DataBinder. Eval (Container.DataItem, "field name", "format")%>
Bind to DataReader:
<%# ((IDataReader) container.dataitem). Field name%>
From: http://hi.baidu.com/jiangyangw3r/blog/item/d8d27b3100a23210eac4af76.html
Dataitem,gridview,repeater Data Control Data binding