How to format the eval output value in the gridview?

Source: Internet
Author: User

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 >

 

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.