Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 4.0 DropDownList with single quotes in text

We have a asp:DropDownList that we populate server side with

ddlBranch.Items.Add(new ListItem("TEST","This is a's test"));

When this is compiled and run under .NET 3.5 we see the text "This is a's test"

However when this is compiled and run under .NET 4.0 we see the text "This is a's test"

We have added the following to our web.config and there was no change.

<pages controlRenderingCompatibilityVersion="3.5" />

For the time being we have dropped back to .NET 3.5 however we would like to know if there is a way to work around this or if this is a known rendering issue or is by design.

TIA

AJ

like image 813
AnthonyJ Avatar asked May 09 '11 01:05

AnthonyJ


1 Answers

Hi All
Thanks for the responses and they led me to look deeper into the code looking for an Encode somewhere. It turns out there that was a:

Server.HtmlEncode(input)

being performed on all controls in a base page class.

Now what I thought was a problem really turns out to be a case of RTFM on my part

From http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes
HtmlEncode and UrlEncode Now Encode Single Quotation Marks

In ASP.NET 4, the HtmlEncode and UrlEncode methods of the HttpUtility and >HttpServerUtility classes have been updated to encode the single quotation mark character >(') as follows:

The HtmlEncode method encodes instances of the single quotation mark as ' . The UrlEncode method encodes instances of the single quotation mark as %27.

So when I was using .NET3.5 my single quote ( ' ) was being ignored by the HtmlEncode but when switching to .NET 4.0 it was not being ignored by HtmlEncode.

Thanks again for all the responses and work that people put in to this question.

Regards

AJ

like image 178
AnthonyJ Avatar answered Oct 06 '22 09:10

AnthonyJ