Differences and usage of data formatting-eval ("") and databinder. eval (container. dataitem, "")
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
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.
Example of formatting data and databinder. Eval
Databinder. Eval has three parameters: the name container of the data item, the name of the data field, and the formatted string. In the template list, such as datalist, DataGrid, or repeater, the named container always container. dataitem. Page is another named container that can be used by databinder. eval.
<% # 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: g2.16.gif ") %> '/>
// </Itemtemplate>
Conversion Type
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
-------------------------------------------------
1. the basic format of databinder. Eval is often used when data is bound. Program : Or Today I learned another method, and Microsoft also said that this method is more efficient than the above two methods. Very useful, so that you can do a lot of things on the front-end page. Remember to import the namespace system. Data on the front-end page. Otherwise, an error message is generated. In fact It is a truth. TEXT =' 'The method is the fastest text =' 'You can also bind the method, but the method is public text =' 'The Key to connecting multiple fields is the container, which is mysterious. Its namespace is system. componentmodel. I need to further understand it. 2. Determine and select databinder. Eval CS defines the dgformatsex method protected string dgformatsex (string xb) {If (XB = "1") Return "male"; else return "female";} databinder. eval usage example databinder. eval usage example // display two decimal places // // {0: g} indicates whether to display true or false ////// Conversion Type (string) databinder. eval (container, "dataitem. p_ship_time_sbm8 ")). substring () {0: d} date only shows year month day {0: yyyy-mm-dd} display year month day {0: c} currency style by format
Dataformatstring usage notes
The syntax of the dataformatstring attribute is as follows:
Dataformatstring = "{0: Format String }"
We know that {0} In dataformatstring represents the data itself, while the format string after the colon represents the format that we want the data to display; in addition, after the specified format symbol, you can specify the number of digits to be displayed in decimal places. For example, if the original data is "1.56" and the format is set to {0: N1}, the output is "1.5 」. The common numeric format is shown in the following table:
Format String input result
"{0: c}" 12345.6789 $12,345.68
"{0: c}"-12345.6789 ($12,345.68)
"{0: d}" 12345 12345
"{0: D8}" 12345 00012345
"{0: e}" 12345.6789 1234568e + 004
"{0: E10}" 12345.6789 1.2345678900e + 004
"{0: f}" 12345.6789 12345.68
"{0: F0}" 12345.6789 12346
"{0: g}" 12345.6789 12345.6789
"{0: G7}" 123456789 1.234568e8
"{0: n}" 12345.6789 12,345.68
"{0: N4}" 123456789 123,456,789.0000
"Total: {0: c}" 12345.6789 total: $12345.68
The commonly used date formats are shown in the following table:
Format description output format
D. Simplified Date Format: mm/DD/YYYY
D detailed Date Format: dddd, Mmmm DD, yyyy
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F
Complete Date and Time Format
(Long date + long time)
Dddd, Mmmm DD, yyyy hh: mm: SS
G General Format (short date + short time) mm/DD/YYYY hh: mm
G General Format (short date + long time) mm/DD/YYYY hh: mm: SS
M, M month/day format mmmm dd
S moderate Date and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS
Character formatting
The date and time are stored in the database, but only the date is displayed on the page. It was previously processed during the query, And the mid (format (renewtime, 'yyyy-mm-dd'), 6, 5) extract the date and display it again. Today, we can see that dataformatstring can be used in datalist and gridview to format and display data in the specified format.
My example:
<Asp: datalist id = "dlistnews" runat = "server">
<Itemtemplate>
<A Title = <% # databinder. eval (container. dataitem, "title") %> Target = "_ blank" href = 'sysbin/news/news_show.aspx? Id = <% # databinder. eval (container. dataitem, "ID") %> '>
<% # Getsubstring (databinder. eval (container. dataitem, "title"). tostring (), 24) %> </a>
(<% # Databinder. eval (container. dataitem, "Showtime") %>)
<% # Databinder. eval (container. dataitem, "renewtime", "{0: d}") %> or
<% # Databinder. eval (container. dataitem, "renewtime ","{0: yyyy-mm-dd}") %>
</Span>
</Itemtemplate>
</ASP: datalist>
If BIND () is used for value, the string in the specified format is also placed behind
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text ='<% # BIND ("addintime", "{0: yyyy-mm-dd }")%> '> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
Control number: dataformatstring = "{0: f}", which is the default format and displays two decimal places.
Value, dataformatstring = "{0: fn.
Usage:
Dataformatstring = "{0: Format String }"
{0} In dataformatstring represents the data itself, while the format string after the colon represents the format they want to display the data;
Number and currency format:
After the specified format symbol, you can specify the number of digits to be displayed in decimal places. For example, if the original data is "1.56" and the format is set to {0: N1 },
Output Value: 1.5 」. The common numeric format is shown in the following table:
Format String input result
"{0: c}" 12345.6789 $12,345.68
"{0: c}"-12345.6789 ($12,345.68)
"{0: d}" 12345 12345
"{0: D8}" 12345 00012345
"{0: e}" 12345.6789 1234568e + 004
"{0: E10 }" 12345.6789 1.2345678900e + 004
"{0: F }" 12345.6789 12345.68
"{0: F0 }" 12345.6789 12346
"{0: g}" 12345.6789 12345.6789
"{0: G7}" 123456789 1.234568e8
"{0: n}" 12345.6789 12,345.68
"{0: N4}" 123456789 123,456,789.0000
"Total: {0: c}" 12345.6789 total: $12345.68
Common Date and Time formats:
Format description output format
D. Simplified Date Format: mm/DD/YYYY
D detailed Date Format: dddd, Mmmm DD, yyyy
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F
Complete Date and Time Format
(Long date + long time)
Dddd, Mmmm DD, yyyy hh: mm: SS
G General Format (short date + short time) mm/DD/YYYY hh: mm
G General Format (short date + long time) mm/DD/YYYY hh: mm: SS
M, M month/day format mmmm dd
S moderate Date and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS
Note: If you use dataformatstring to format the gridview,
Set htmlencode = false to make dataformatstring take effect.
<Asp: boundfield headertext = "Total" datafield = "Total" dataformatstring = "{0: c}" htmlencode = "false">