Updated: 14/12/2005; 9:54:43.
Hannes 'Shorty' Decorte's Blog | SharePoint, .Net 2.0, ...
Bringing SharePoint, Office and .Net information


woensdag 16 november 2005

 

Version: ASP.net 2.0 RTM

I wasted a few minutes figuring out this one.

You have a BoundField object bound to a field of type DateTime with a DataFormatString attribute but the format string is not being applied.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" />

Instead, the field appears to be formatted using its ToString()method like so:


Output: 10/31/2005 7:00:54 PM
 

Cause

To prevent cross site scripting attacks, the field value is HtmlEncoded. The HtmlEncoding occurs before applying the DataFormatString making the format string have no effect.

 

Resolution

In this case (ie. when using a field of type DateTime), set HtmlEncode to false.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

 

Output: 10/31/2005

I am not sure why the ASP.net team decided against HtmlEncoding the string after applying the DataFormatString.


3:26:35 PM    comment []

© Copyright 2005 Hannes 'Shorty' Decorte.
 
November 2005
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Sep   Dec


RSS Feed


Other .Net Bloggers

Bart De Smet
Jan Tielens
Patrick Verbruggen
Patrick Tisseghem
Tom van de Kerkhof's Bloggings
Tom's Corner
Yves Hanoulle

Aggregated Feeds

Belgian .NET Bloggers