ASP.NET 4.0 DropDownList with single quotes in text

心不动则不痛 提交于 2019-12-04 23:17:27

问题


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.



来源:https://stackoverflow.com/questions/5931605/asp-net-4-0-dropdownlist-with-single-quotes-in-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!