Data formatting in .net 1.1



  • Morning.

    Im having a problem with the string.format on my site...
    Basically its wrinting a £ sign to the page and I want it to output the HTML value £ instead...

    I have written a custom format provider for it so that I can do the following:

    [red]StringFormatInfo inf = new StringFormatInfo();
    Console.WriteLine(string.Format(inf, "{0:c}", 12.00));[/red]

    This works fine when invoked from the code behind in tha page, but in may places I have bits like this:

    [red]<asp:BoundColumn DataField="one" HeaderText="One" DataFormatString="{0:ht}"></asp:BoundColumn>[/red]

    My problem is trying to work out how to get the outout of this changed.. I thought that it was something to do with overriding the DataFormatString of the boundColumn... but got lost at this stage...

    am I on the right path? If so any pointers?
    If Im going about this the wrong way then what is the right way?

    Thanks

    David



  • @wonkoTheSane said:

    Morning.

    Im having a problem with the string.format on my site...
    Basically its wrinting a £ sign to the page and I want it to output the HTML value &pound; instead...

    Why? An actual £ sign is valid html. So what’s the problem?



  • He probably wants to cover every other browser possible. Though, since the three major ones (IE, FF, Mozilla) already cover this, he's probably going for mobile browsers as well.

     Dude. You've already got your custom formatter and now you want to change the output of something via the dataformatstring of a GridView column? I don't get it, could you elaborate further on it? (it migh tbe me, I'm not a native speaker).



  • Regardless, ASP (if it's worth anything - it might not be, but in that case he shouldn't be using it at all) already handles this, and knows better than you when something does or does not need to be converted.

    I'll bet anything that if you change your formatter to return "&pound;", you will see precisely that in the page (i.e. the raw html output will be "&amp;pound;"). 



  • This format string seems to work fine for me in .NET 2.0:

    {0:&pound;#,##0.00;(&pound;#,##0.00);&pound;#,##0.00}

    1560.20 -> &pound;1,560.20
    -1560.20 -> (&pound;1,560.20)
    0 -> &pound;0.00

    I'm not sure if the BoundColumn will attempt to HTML encode the ampersand into &amp;. In ASP.NET 2.0, you have to set the HtmlEncode property of the BoundColumn to False in order to use a DataFormatString, so investigate that if necessary.



  • @db2 said:

    This format string seems to work fine for me in .NET 2.0:

    {0:&pound;#,##0.00;(&pound;#,##0.00);&pound;#,##0.00}

    1560.20 -> &pound;1,560.20
    -1560.20 -> (&pound;1,560.20)
    0 -> &pound;0.00

    I'm not sure if the BoundColumn will attempt to HTML encode the ampersand into &amp;. In ASP.NET 2.0, you have to set the HtmlEncode property of the BoundColumn to False in order to use a DataFormatString, so investigate that if necessary.

    Is there really no way to make it do the HTML encoding _after_ the format string, so that you can return a literal £ and it will output &pound;?

    You'd think that would be the default behavior. 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.