MSDN lies?! Or maybe a much deeper WTF?



  • https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.text(v=vs.110).aspx

    The Text property can include HTML. If it does, the HTML will be passed unchanged to the browser, where is might be interpreted as markup and not as text.

    Oh really? Then why does my webpage say:

    Foo<br />Bar<br />Baz<br />

    Either that, or this is configurable some... how? and the MSDN page doesn't mention you can change that behavior and I have no idea where to look.


  • :belt_onion:

    Am I blind, or

    If you want the browser to display HTML markup as plain text, you can use the HtmlEncode method, as shown in the example for the class overview.

    this part is false? I don't browse the site usually, but the only example I see is below, and it doesn't use that method.

    Also, maybe it's confused by XHTML syntax and encodes stuff by itself due to that? Or is that a standard thing to use in .NET?


  • sockdevs

    Seems this is the key bit

    where it might be interpreted as markup and not as text

    So it's not guaranteed to show as HTML; seems it's up to the browser



  • How do you set the label text? Some variation of <% tags encodes by default, and there are probably some more ways to enable encoding by default.



  • Just MyLabel.Text = "Foo&lt;br />Bar";

    @RaceProUK said:

    So it's not guaranteed to show as HTML; seems it's up to the browser

    How do I make it guaranteed? I want the tags.


  • :belt_onion:

    Ok, I might be the silly one here: this still basically works like classic ASP or PHP, no? As in, server executes your code and the output is pure HTML, no? Meaning, if the browser got </> instead of &lt;/&gt; it will parse it as HTML. Even if you give it XHTML syntax in an HTML file.

    The only other possibility is that it's async and that the JS fucks up somewhere. I know I had some combination of FF and jQuery do that to me once, fuck knows why.

    Aaand...

    @blakeyrat said:

    Just MyLabel.Text = "Foo&lt;br /&gt;Bar";

    Is that a Discourse fuckup or you really have an &lt; in there? Because that's probably what it's doing it. From what I gathered from the docs, it just passes the text, it won't turn &lt; into <.



  • It's a bit more complicated sometimes - for example, GridView is HTML-encoded by default, TextBox returns encoded output, and so on. But in Blakey's case, if it's actually <, then I'm just as confused - we stuff random HTML snippets into labels all the time...

    Depending on the WTFness of the codebase, there might be some hidden hook on the setter, but I'd be amazed if you could do that.


  • :belt_onion:

    @Maciejasjmj said:

    It's a bit more complicated sometimes - for example, GridView is HTML-encoded by default, TextBox returns encoded output, and so on. But in Blakey's case, if it's actually <, then I'm just as confused - we stuff random HTML snippets into labels all the time...

    Ok, that makes sense, but that's just what classes do serverside: as long as the browser gets proper tags it should render them, no "might" about it was my point.

    But for the use he needs it for, and from the docs, I expect no such chicanery on a label element.



  • It does JS magic too occasionally (live validation, for example), but in this case I know of nothing that could screw this up.

    Some odd parent element looping through children and encoding? Or other spooky actions at a distance.



  • That's a Discourse fuckup, the backtick code thing apparently breaks HTML entities.



  • @Maciejasjmj said:

    Depending on the WTFness of the codebase, there might be some hidden hook on the setter, but I'd be amazed if you could do that.

    Debugger says there ain't one. Maybe there's some kind of "output filter" that's running?



  • @blakeyrat said:

    I want the tags.

    Use a Literal control instead of a Label.



  • Right but why is the doc a liar?



  • @blakeyrat said:

    why is the doc a liar?

    Shitty open sores quality control, most likely.



  • Nah, this dates back to the shared sores (eww) or even closed sores days.


Log in to reply
 

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