When using the gridview in ASP. NET, custom templates are often used (because the default one does not meet our needs). When using custom templates, Eval is often indispensable to output field values.
But the eval content is not necessarily satisfactory to us. We may have to format it before outputting it.
< ASP: templatefield headertext = " Price " >
< Itemstyle cssclass = " Price " />
< Itemtemplate > <% # String. Format ( " {0.00} " , Eval ( " Price " )) %> </ Itemtemplate >
</ ASP: templatefield >
As shown above, the value of the price field is formatted by string. Format and output. By the way, {0.00:} indicates the following meaning:
- The price value is reserved for two decimal places (rounded to five). If there are less than two decimal places, the value is set to zero.
- Price indicates the decimal point. If the decimal point (two digits) is less than five digits, add a blank space to the front to supplement the five digits.
Let's look at the date
< Itemtemplate > <% # Convert. todatetime (eval ( " Expiredate " ). Tostring ( " Yyyy-mm-dd " ) %> </ Itemtemplate >
Let's take a look at
Previously, we used the internal method of the system. to customize the method, we must first define at least the protected method, for example:
Protected String Foo ( String Str)
{
Return Str + " " ;
}
During the application, you must note that the eval field type is string. For example:
< Itemtemplate > <% # Foo (eval ( " Realname " ). Tostring ()) %> </ Itemtemplate >