Bind is a two-way binding, but requires that the data source be changed to be used. ASP.net 2.0 improves data-binding operations in the template, simplifying the data-binding syntax DataBinder.Eval (Container.DataItem, fieldname) in v1.x to Eval (fieldname). The Eval method, like DataBinder.Eval, can accept an optional format string parameter. The difference between the shortened eval syntax and the DataBinder.Eval is that Eval automatically parses the fields based on the DataItem properties of the nearest container object (such as DataListItem), and DataBinder.Eval needs to use parameters to specify the container. For this reason, eval can only be used in a data-bound control's template, not the page layer. Of course, DataBinder.Eval is still supported on the ASP.net 2.0 page, and you can use it in an environment that does not support simplified Eval syntax.
The following example shows how to use the new simplified eval data binding syntax to bind to the image, label, and Hyperlink controls in the DataList data item template (ItemTemplate).
The following are the referenced contents: = "5" width= "runat=" "Server" DataSourceID = "ObjectDataSource1" > = "Server" Navigateurl= ' <%# Eval ("PhotoID", "Photoformviewplain.aspx?id={0}")%> ' > = "Server" Imageurl= ' <%# Eval ("FileName", "Images/thumbs/{0}")%> '/> = ' <%# Eval ("Caption")%> '/> = "Server" TypeName = "Datacomponenttableadapters.photostableadapter "Selectmethod=" Getphotosforalbum " |
Bindings can also be part of the subject definition of a control (theme definition) so that we can change the layout and appearance of the templated control arbitrarily by changing the theme. However, the theme (subject) template can only use eval (or the bind discussed later). Binding to arbitrary user code is prohibited.