ASP.NET Gripe #4

This one’s fairly minor because I can understand why Microsoft have done it this way. Still, it’s bugging me this morning so here goes…
 
For controls such as list boxes, the contents are automatically passed through an HtmlEncode function, which means that if you have, say, a ListItem with a value of "This & that", it will actually be written out to the page as "That & that". Now I’d say that’s sensible enough – it means you don’t have to do that kind of thing yourself when putting data into lists, and it’s especially useful if you’re binding your controls to a DataSource.
 
On the other hand, though, if you have a label control and do:
MyLabel.Text = "That & that";
…what appears on the page is "This & that". In other words, if you want to have the value passed through Server.HtmlEncode, you have to do it yourself.
 
I’ve previously had a problem with some of the data I get from SAP, in that there’s a division called "NAT&REG". Spot the problem? The "&reg" bit is the HTML code for the registration symbol, ®, and so what comes out in your browser is "NAT®’. (Yes, I know the &reg bit is supposed to have a semicolon on the end, but IE doesn’t need it).
 
So my minor gripe is: if one set of controls does automatic escaping, why doesn’t the other? After all, you can bind labels to a dataset too.
Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s