ASP. NET 2.0 improves the Data Binding operation in the template. It simplifies 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 formatted string parameter. Shortened eval syntax and databinder. the difference between Eval is that Eval will automatically parse fields based on the dataitem attribute of the most recent container object (such as datalistitem), while databinder. eval needs to use parameters to specify the container. For this reason, Eval can only be used in the template of the data-bound control, but cannot be used in the page layer. Of course, the ASP. NET 2.0 page still supports databinder. Eval. You can use it in environments that do not support simplified eval syntax.
Summary of databinder. Eval usage in Asp.net
<% # BIND ( " Subject " ) %> // Bind a field
<% # Container. dataitemindex + 1 %> // Implement automatic numbering
<% # Databinder. eval (container. dataitem, " [N] " ) %>
Common method (the best performance of these three methods)
<% # Databinder. eval (container. dataitem, " Columnname " ) %>
<% # Databinder. eval (container. dataitem, " Columnname " , Null ) %>
<% # Databinder. eval (container, " Dataitem. columnname " , Null ) %>
Other usage
<% # (Datarowview) container. dataitem )[ " Columnname " ] %>
<% # (Datarowview) container. dataitem). Row [ " Columnname " ] %>
<% # (Datarowview) container. dataitem )[ " Adtitle " ] %>
<% # (Datarowview) container. dataitem) [N] %>
<% # (Dbdatarecord) container. dataitem )[ 0 ] %>
<% # (Custom type) container. dataitem). Attribute. tostring () %> // If the attribute is of the string type, tostring () is not required.
Example of databinder. Eval usage
<% # Databinder. eval (container. dataitem, " Integervalue " , " {0: c} " ) %>
The format string parameter is optional. If the parameter is ignored, databinder. Eval returns the object type value,
// Display two decimal places
<%# Databinder. eval (container. dataitem,"Unitprice","$ {0: F2}")%>
// {0: g} indicates that true or false is displayed.
<Itemtemplate>
<ASP: ImageWidth= "12"Height= "12"Border= "0"Runat= "Server"
Alternatetext= '<% #Databinder. eval (container. dataitem, "discontinued", "{0: g}") %>'
Imageurl ='<%# Databinder. eval (container. dataitem,"Discontinued","~ /Images/{0: g2.16.gif")%>'/>
</Itemtemplate>
// Conversion Type
(String) databinder. eval (container, "dataitem. p_ship_time_sbm8"). substring (4, 4)
{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format
{0: c} currency Style
<% # Container. dataitem ( " Price " , " {0: ¥ #, #0.00} " ) %>
<% # Databinder. eval (container. dataitem, " Company_ureg_date " , " {0: yyyy-m-d} " ) %>
Specifier Type format output (passed double 1.42) output (passed int-12400)
C currency {0: c} $1.42-$12,400
D decimal {0: d} system. formatexception-12400
E scientific {0: e} 1.420000e + 000-1.2420.e + 004
F fixed point {0: f} 1.42-12400.00
G General {0: g} 1.42-12400
N number with commas for thousands {0: n} 1.42-12,400
R round trippable {0: R} 1.42 system. formatexception
X hexadecimal {0: X4} system. formatexception cf90
{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format
<% # Eval ("stime", "{0: yyyy-mm-dd}") %>
the style depends on the web. settings in config
{0: c} or {0: £ 0,000.00} currency style standard British currency style
System . web
globalization requestencoding = "UTF-8" responseencoding = "UTF-8" culture = "En-us" uiculture = "En-us" />
system. web
3,000.10
{0: c} or string. format ("{0: c}", price); Chinese currency style
System . web
globalization requestencoding = "UTF-8" responseencoding = "UTF-8" culture = "ZH-CN" uiculture = "ZH-CN" />
system. web >
displayed as ¥3,000.10
{0: c} or string. Format ("{0: c}", price); American currency Style
<System. Web>
<GlobalizationRequestencoding= "UTF-8"Responseencoding= "UTF-8" />
</System. Web>
Shown as $3,000.10
Practice is required.