Curtis
Curtis

Reputation: 103348

Why won't this Boolean string format work?

I currently have the following GridView TemplateField:

<asp:TemplateField HeaderText="Despatched">
    <ItemTemplate><%# IIf(Eval("Despatched"), "Yes", "No")%></ItemTemplate>
</asp:TemplateField>

This works fine, but I've read I can do the following instead, which I think is a lot neater:

<asp:BoundField HeaderText="Despatched" DataField="Despatched" DataFormatString="{0:Yes;No}" />

However, this still returns True/False values instead of Yes/No

Why isn't this working?

Thanks.

Upvotes: 2

Views: 2650

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You should add HtmlEncode="false" to your BoundField with DateFormatString:

<asp:BoundField HeaderText="Despatched" DataField="Despatched" DataFormatString="{0:Yes;No}" HtmlEncode="false" />

http://weblogs.asp.net/rajbk/archive/2005/10/31/boundfield-dataformatstring-attribute-not-being-applied.aspx

Upvotes: 1

Simon Mourier
Simon Mourier

Reputation: 138915

AFAIK, bool.ToString() does not support a custom formatting string such as {0:Yes;No} although I truly admit it would be a good idea :-)

The following code:

    bool b = true;
    Console.WriteLine("{0:Yes;No}", b);

Always display True whatever .NET framework version (2, 3, 4) you use.

Upvotes: 8

ChrisLively
ChrisLively

Reputation: 88064

The code you have is accurate. You might try cleaning and recompiling the site to see if the behavior changes. If this is from a deployment, delete the destination before copying the new files out.

I just get the feeling that visual studio didn't update your .aspx file on a deployment. Which I've found tends to happen a little too frequently.

Upvotes: -1

Related Questions