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.
Databinder. summary of eval usage <% # BIND ("subject") %> // bind a field <% # container. dataitemindex + 1%> // implement automatic number <% # databinder. eval (container. dataitem, "[N]") %> commonly used methods (which have the best performance) <% # databinder. eval (container. dataitem, "columnname") %> <% # databinder. eval (container. dataitem, "columnname", null) %> <% # databinder. eval (container, "dataitem. columnname ", null) %> other usage <% # (datarowview) container. dataitem) ["Colum Nname "] %> <% # (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, you do not need tostring (). databinder. eval usage example <% # 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 () {0: d} date only shows year month day {0: yyyy-mm-dd} display year month day {0: c} currency style by format <% # 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.00g 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
The {0: d} date only displays the year, month, and day. {0: yyyy-mm-dd} displays the year, month, and day in the format.
The style depends on the web. set {0: c} or {0: 00000,000.00} currency style in config. Standard British currency style <system. web> <globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "En-us" uiculture = "En-us"/> </system. web> display 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> display 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> displayed as $3,000.10
Practice is required.
Example of formatting data and databinder. Eval usage databinder. Eval has three parameters: the name container of the data item, the name of the data field, and the formatting 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: gw..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.00g 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
The {0: d} date only displays the year, month, and day. {0: yyyy-mm-dd} displays the year, month, and day in the 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> display 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> display 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> displayed as $3,000.10
-------------------------------------------------
1. databinder. the basic format of eval often uses this sentence when binding data: 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. This usage is actually a truth. TEXT = ''is the fastest way to bind text =''. However, if the public text = ''method is used, the key to connecting multiple fields is the container, it is mysterious. Its namespace is system. componentmodel. I need to further understand it. 2. databinder. eval implementation judgment select the dgformatsex method defined in CS protected string dgformatsex (string xb) {If (XB = "1") Return "male"; else return "female";} databinder. eval usage example databinder. eval usage example // show two decimal places //// {0: g} indicates to show 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, the format string after the colon represents the format of the data to be displayed. 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 commonly used 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 following table lists common date formats: format description output format D simplified Date Format mm/DD/yyyy d detailed Date Format dddd, Mmmm DD, yyyy F complete format (long date + short time) dddd, Mmmm DD, yyyy hh: mm f full 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 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: specify the number of digits to be displayed after the specified format symbol. 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 complete format (long date + short time) dddd, Mmmm DD, yyyy hh: mm f full 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 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 and set htmlencode = false, the dataformatstring will take effect. <asp: boundfield headertext = "Total" datafield = "Total" dataformatstring = "{0: c}" htmlencode = "false">
Certificate -----------------------------------------------------------------------------------------------------------------------------------------
Databinder: Data Binding manager Eval: Evaluate container: container to be bound, such as gridview, datalist, and other dataitem: container data items, including items, alternate template line shipname: the fields bound to the container (from the database table field) are extended to you: eval ("") and bind ("") one-way binding and one-way binding.
BIND is a two-way binding, but can be used only when the data source can be changed
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.
The following example demonstrates how to bind a new simplified eval Data Binding syntax to the image, label, and hyperlink controls in the datalist data item template (itemtemplate.
<Asp: datalist id = "datalist1" repeatcolumns = "5" width = "600" runat = "server" performanceid = "objectperformance1"> <itemtemplate> <asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = & apos; <% # eval ("photoid", "photoformviewplain. aspx? Id = {0} ") %> & apos;> <asp: Image id =" image1
Differences and usage of data formatting-eval ("") and databinder. eval (container. dataitem, "")