ASP.NET 4.0 DropDownList with single quotes in text
Asked Answered
S

3

6

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

Soult answered 9/5, 2011 at 1:28 Comment(1)
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. See asp.net/learn/whitepapers/aspnet4/breaking-changes HtmlEncode and UrlEncode Now Encode Single Quotation Marks about the changes in .NET 4 regarding it now recognising the single quote in HTMLEncode. Thanks for all the input from everyone.Soult
S
2

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

Soult answered 10/5, 2011 at 9:49 Comment(0)
M
0

When you get the value back you could just HTMLDecode the selected value.

ie. Server.HtmlDecode(ddlBranch.SelectedValue)

Mozzetta answered 9/5, 2011 at 3:14 Comment(1)
You don't actually have to decode it in the codebehind. The DropDownList control will handle that for you.Inscrutable
D
0

Why do you believe this is a problem? &#39; renders as an apostrophe, and when posted will turn into an apostrophe if that value is selected.

Drusilladrusus answered 9/5, 2011 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.