ASP.NET 4.0 DropDownList with single quotes in text

We have asp: DropDownList, which we populate on the server side

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

When it compiles and runs under .NET 3.5, we see the text "This is a test"

However, when it compiles and runs under .NET 4.0, we see the text "This is a test ' "

We added the following to our web.config, and there were no changes.

 <pages controlRenderingCompatibilityVersion="3.5" /> 

We are currently back to .NET 3.5, however we would like to know if there is a way around this or if this is a known rendering or design issue.

TIA

Aj

+6
source share
3 answers

Hi everyone Thanks for the answers and they made me look deeper into the code looking for somewhere Encode. Turns out it was:

 Server.HtmlEncode(input) 

runs for all controls in the base class.

Now what I thought was the problem actually turns out to be in the case of RTFM on my part

From http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes
HtmlEncode and UrlEncode now encode separate quote labels

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

The HtmlEncode method encodes single-quote instances as'. The UrlEncode method encodes single quote instances as% 27.

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

Thanks again for the answers and work that people ask this question.

Hi

Aj

+2
source

When you get the value back, you can just HTMLDecode the selected value.

T. Server.HtmlDecode(ddlBranch.SelectedValue)

0
source

Why do you think this is a problem? & Amp; # 39; is displayed as an apostrophe, and when it is sent, it will turn into an apostrophe if this value is selected.

0
source

Source: https://habr.com/ru/post/887584/


All Articles