How to use the template column in The gridview Date Format

Source: Internet
Author: User
In Asp.net 2.0, if you want to display data in the binding column, such as the date format, the following method cannot be used.

<Asp: boundfield datafield = "creationdate"

Dataformatstring = "{0: M-dd-yyyy }"

Headertext = "creationdate"/>

The main reason is that the htmlencode attribute is set to true by default, which has prevented XSS attacks and is used for security reasons. Therefore, there are two solutions:

1,

<Asp: gridview id = "gridview1" runat = "server">

<Columns>

<Asp: boundfield datafield = "creationdate"

Dataformatstring = "{0: M-dd-yyyy }"

Htmlencode = "false"

Headertext = "creationdate"/>

</Columns>

</Asp>

Set htmlencode to false.

Another solution is to use the template Column

<Asp: gridview id = "gridview3" runat = "server">

<Columns>

<Asp: templatefield headertext = "creationdate">

<Edititemtemplate>

<Asp: Label id = "label1" runat = "server"

TEXT = '<% # eval ("creationdate", "{0: M-dd-yyyy}") %>'>

</Asp>

</Edititemtemplate>

<Itemtemplate>

<Asp: Label id = "label1" runat = "server"

TEXT = '<% # BIND ("creationdate", "{0: M-dd-yyyy}") %>'>

</Asp>

</Itemtemplate>

</Asp>

</Columns>

</Asp>

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.