Flexible application of data binding operations
Binding to Simple properties: <% #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 you have an object in the array, you may want to make the necessary transformations and then bind to the following:
<%# ((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:
<% #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, in order to facilitate the most commonly used is the DataBinder class of the Eval method. But this is less efficient at the same time to bind a large amount of data
This procedure is often used when binding data: <%# DataBinder.Eval (Container.DataItem, "xxxx")%> or <%# DataBinder.Eval (Container, " Dataitem.xxxx ")%>
I learned another one today, and Microsoft says the method is more efficient than both.
<%# ((DataRowView) container.dataitem) ["XXXX"]%>
Very useful, so you can do a lot of things on the front page.
Also remember to do this if you have to import the namespace System.Data on the foreground page, or you will generate an error message.
<%@ Import namespace= "System.Data"%>
This usage actually and <%# ((DictionaryEntry) Container.DataItem). Key%> is a truth.
When binding to a DataSet, DataTable:
<%# ((System.Data.DataRowView) container.dataitem) [Field name]%>
<%# ((System.Data.DataRowView) container.dataitem) [Index]%>
When binding to DataReader:
<%# ((System.Data.Common.DbDataRecord) container.dataitem) [Index]%>
<%# ((System.Data.Common.DbDataRecord) container.dataitem) [Field name]%>
The key is to container this thing, it is more mysterious. Its namespace is System.ComponentModel. I need to understand more about it.
Beginner. NET, now looking at the DataGrid control, when ItemTemplate display data,
What is the difference between DataBinder.Eval (Container.DataItem, "name") and Container.DataItem ("name").
DataBinder is a static class inside system.web that provides an eval method for simplifying the writing of data-binding expressions, but it is used in ways that are easy to use through expensive methods such as reflection, so their performance is not the best. And container is not at all a static object or method, It is a local variable declared inside the data-bound event handler by the asp.net page compiler whose type is the data container type of the control that can be data-bound (for example, the data binding container within the repeater is called RepeaterItem), and there are basically dataitem properties in those container classes, so you can write C Ontainer. DataItem, this property returns the data item in the data source that you are binding to. If your data source is a DataTable, the type of the data item is actually DataRowView.