in

Corey Roth and Friends Blogs

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

Cory Robinson's Blog

HtmlEncodeFormatString errors in GridView BoundField after precompile

I ran across an issue today where my data format string was not being applied to my data in a gridview.  It worked locally, but not in dev or test.  Here's what I had:

<asp:BoundField DataField="GrandTotal" HeaderText="Grand Total" ReadOnly="True" SortExpression="GrandTotal" DataFormatString="{0:c}" />

 

Ok what would make this render differently in dev?  I looked at the HtmlEncodeFormatString property of the BoundField, noticing that setting it to false made the formatting go away locally as well.  So I assumed that I needed to add HtmlEncodeFormatString="true" in dev to make it work.  However, I got the following error message:

 

Error: Type 'System.Web.UI.WebControls.BoundField' does not have a public property named 'HtmlEncodeFormatString'.

 

No idea why this is happening.  I googled the error but found nothing useful.  There is an issue with ASP.NET 3.5 and this property defaulting to true, but I don't have 3.5 installed on the dev box.  So that can't be it.

 

So I had the bright idea to convert this BoundField to a template column (by clicking the linkbutton in the Edit Columns designer).  I got this:

 

<asp:TemplateField HeaderText="Grand Total" SortExpression="GrandTotal">

  <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("GrandTotal", "{0:c}") %>'></asp:Label>

  </ItemTemplate>

</asp:TemplateField>

 

Magically, this works both locally and in dev.  I have no idea why.

Comments

 

jonefer said:

You found an alternate way to fix it.

I found this at: weblogs.asp.net/.../429090.aspx

It explains that this is a design flaw by microsoft.

BoundField DataFormatString attribute not being applied.

Version: ASP.net 2.0 RTM

I wasted a few minutes figuring out this one.

You have a BoundField object in a GridView 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"/>

The Output renders as: 10/31/2005

This setting also applies to the HeaderText.

The following:

HeaderText="Employee<BR/>Name"

will not render a line break unless you set HtmlEncode to false.

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

March 29, 2008 1:11 AM
 

29er said:

i have found that HtmlEncodeFormatString property does not work at all. so i just use HtmlEncode and it still works fine in 3.5

November 11, 2010 3:08 PM

Leave a Comment

(required)
(optional)
(required)
Add
2019.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems