ASP.NET 4.0 DropDownList with single quotes in tex

2019-03-27 20:56发布

问题:

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

回答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.

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



回答2:

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

ie. Server.HtmlDecode(ddlBranch.SelectedValue)



回答3:

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.