Detailed usage rules of DataBinder. Eval

Source: Internet
Author: User

<% # Bind ("Subject") %> // Bind a field
<% # Container. DataItemIndex + 1%> // implement automatic numbering
<% # DataBinder. Eval (Container. DataItem, "[n]") %>

Common method
<% # 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). Attributes. 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: Image Width = "12" Height = "12" Border = "0" runat = "server"
AlternateText = <% # DataBinder. Eval (Container. DataItem, "Discontinued", "{0: G}") %>
ImageUrl = <% # DataBinder. Eval (Container. DataItem, "Discontinued ","~ /Images/{0: gw..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


The style depends on the settings in Web. config.

{0: c} or {0: 00000,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>
Displayed as listen 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>
Shown as ¥3,000.10

{0: c} or string. Format ("{0: C}", price); American currency Style
<System. web>
<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8"/>
</System. web>
Shown as $3,000.10

 

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.

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.