ASP.NET Multiline textbox allowing input above UTF-8

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:08:55

问题


In my web.config I have

<globalization
  fileEncoding="utf-8"
  requestEncoding="utf-8"
  responseEncoding="utf-8"
  culture="en-US"
  uiCulture="de-DE"
/>

In my page directive I have

ResponseEncoding="utf-8"

Yet for some reason an ASP TextBox with the TextMode="MultiLine" allows inputs of characters outside of UTF-8. When I paste the following line of text into an ASP TextBox that is not MultiLine

“test”

the non UTF-8 characters are replaced, but not when I use a MultiLine TextBox.

Any ideas why?

EDIT: To explain a little more the set up I am seeing this problem in, here are 4 text areas that can be put on an ASP page.

<asp:TextBox ID="txtTest1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtTest2" runat="server" TextMode="MultiLine"></asp:TextBox>
<input id="Text1" runat="server" />
<textarea id="Textarea1" cols="100" rows="8" runat="server"></textarea>

If you make a page with the ResponseEncoding in your page directive, your web.config with the globalization tab described above, and copy and paste the test line, with quotes, into each of these 4 different types of text areas, why does the font come up different?


回答1:


Just to add some clarification... UTF-8 is a character encoding scheme that covers all Unicode characters. Therefore there isn't any such thing as a "non UTF-8 character."

The encoding of a string has nothing to do with the graphical representation of those characters on the screen (e.g. in a web page's <input type="text"> or <textarea></textarea> controls). In your example, some fonts display the typographer's quotes as straight quotes while others display the very same UTF-8 character as curly quotes.

The ResponseEncoding setting determines what bytes are transmitted to represent the characters that make up the HTML of your page and the characters encoded in form posts and URLs back to your page. Common encodings are UTF-8, ISO 8859-1 and windows-1252. These encodings have a lot of similarities, but they also have their differences. However, you can deliver the very same page using the windows-1252 character set and encoding (which also includes those curly quote characters) and you'd see exactly the same result.

So in a nutshell, don't confuse character encoding with font styles.

By the way, your ResponseEncoding="utf-8" directive is redundant since the same thing is set in web.config. And UTF-8 is the default anyway, so you may not even need it in your web.config.




回答2:


What you are seeing is the different default fonts that are applied to the text area (multiline textbox) verse textbox (input). Text areas use Courier New and single line textboxes use Arial. If you apply a style that sets the font-family to be the same for both the textbox and the textarea then your pasted text will match. Try these and you should see that the pasted contents all match exactly:

<asp:TextBox ID="txtTest1" runat="server" style="font-family: Courier New;"></asp:TextBox>
<asp:TextBox ID="txtTest2" runat="server" TextMode="MultiLine" style="font-family: Courier New;"></asp:TextBox>
<input id="Text1" runat="server" style="font-family: Courier New;" />
<textarea id="Textarea1" cols="100" rows="8" runat="server" style="font-family: Courier New;"></textarea>


来源:https://stackoverflow.com/questions/2215547/asp-net-multiline-textbox-allowing-input-above-utf-8

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