Digital Colony!

DataFormatString Not Working? Here is the Fix

If you have a GridView control and are are trying to data format a BoundField, you may find your format instructions are being ignored. The fix is to add HtmlEncode="False" to the asp:BoundField. Then the DataFormatString should work.
<asp:BoundField DataField="LastModified" 
  HeaderText="Modified" 
  HtmlEncode="False" 
  DataFormatString="{0:d}" />

Labels:

AddThis Social Bookmark Button

20 Comments:

Anonymous Anonymous said...

Thank you very much. Who would say that ? Next big step towards self knowledge and nirvana: why ?

3/28/2007 9:06 PM

Anonymous Ben said...

I have a GridView in a Web User Control (.ascx file) and this doesn't work. Any suggestions?

5/17/2007 12:59 PM

Blogger MAS said...

On vacation now. Will play w/ gridview in control on Tuesday.

5/17/2007 10:15 PM

Blogger Aman said...

Great!!!!!!!!

6/21/2007 3:12 AM

Blogger Pete said...

*THUNDERING APPLAUSE*

7/05/2007 11:44 PM

Anonymous Anonymous said...

How INSANE that this isn't addressed anywhere in Microsoft's Web Dev tool's documentation. Good grief! Thanks for the fix -- was about to make a forehead-shaped dent in my desk! :-)

8/18/2007 2:34 PM

Anonymous Anonymous said...

HTMLEncode = false!!!!! thx for this because i was searching how to resolve the problem with the day without time in GridView all the day! Thx god i finally faund your answer :)

8/31/2007 2:39 AM

Anonymous Anonymous said...

Thank you so much. Saved me trouble.

9/04/2007 8:18 AM

Anonymous Anonymous said...

Thanks a lot. I spent more then one day for this!!!!!!!!!!!!!!

9/27/2007 7:22 PM

Anonymous Anonymous said...

Thanks, this saved me hours of banging my head against my monitor.

10/13/2007 11:19 AM

Blogger Kal said...

Thank you so much, or should I say "Merci Beaucoup !"

10/24/2007 9:11 AM

Anonymous Mik said...

Thank you, saved me time :)

12/05/2007 5:17 PM

Anonymous Anonymous said...

I have been looking for a solution to this problem for ages! Thanks a lot.

12/11/2007 1:32 AM

Anonymous Anonymous said...

that was cool!! would have never thoought about it !htmlencode=false!!!who would have thought!!

12/14/2007 9:19 AM

Anonymous Prashant Atal said...

Thanks a lot for your post as it helped me save a lot of my time. It was working on my development machine and not on my production server and i was about to shoot myself for not able to resolve such a small thing.

But it seems that i need to shoot the MS guys :).

12/16/2007 11:57 PM

Blogger All Within My Hands said...

I'm using VB.Net in my ASP.Net page, and I don't have the HtmlEncode attribute in either the HTML or VB code. Any ideas?

1/09/2008 7:15 AM

Blogger MAS said...

The HTMLEncode is an attribute of the ASP.NET control on the .ASPX page. It makes no difference if you use VB.NET or C#.

1/09/2008 7:25 AM

Blogger All Within My Hands said...

Why won't it work for me? The server is running .net 2.0 SP1, and I'm using the standard datagrid control.

1/09/2008 9:32 AM

Blogger MAS said...

Since you are using ASP.NET 2.0, use a GridView control instead of a DataGrid.

1/09/2008 9:34 AM

Anonymous Anonymous said...

HtmlEncode Enabled, this is because of security reasons. HtmlEncode makes sure (at least in some manners) that XSS-attacks are not possible. HtmlEncode should always be on.

In my opinion, a better solution is to use a TemplateField and perform the formatting in code behind.

1/15/2008 6:19 AM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.