ASP.NET 多行文本框允许输入高于 UTF-8 的内容

发布于 2024-08-20 08:14:05 字数 945 浏览 5 评论 0原文

在我的 web.config 中,

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

在我的页面指令中,

ResponseEncoding="utf-8"

由于某种原因,带有 TextMode="MultiLine" 的 ASP 文本框允许输入 UTF-8 之外的字符。当我将以下文本行粘贴到非 MultiLine 的 ASP 文本框中时,

“test”

非 UTF-8 字符将被替换,但当我使用 MultiLine TextBox 时则不会。

有什么想法吗?

编辑: 为了更多地解释一下我遇到这个问题的设置,这里有 4 个可以放在 ASP 页面上的文本区域。

<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>

如果您在页面指令中使用 ResponseEncoding 创建一个页面,在 web.config 中使用上述全球化选项卡,并将带有引号的测试行复制并粘贴到这 4 种不同类型的文本区域中,为什么字体出现不同的情况吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬シ愛 2024-08-27 08:14:05

只是添加一些说明... UTF-8 是一种涵盖所有 Unicode 字符的字符编码方案。因此,不存在“非 UTF-8 字符”之类的东西。

字符串的编码与屏幕上这些字符的图形表示无关(例如,在网页的 控件中)。在您的示例中,某些字体将印刷者的引号显示为直引号,而其他字体则将完全相同的 UTF-8 字符显示为弯引号。

ResponseEncoding 设置确定传输哪些字节来表示构成页面 HTML 的字符以及在表单帖子和返回页面的 URL 中编码的字符。常见的编码有 UTF-8、ISO 8859-1 和 windows-1252。这些编码有很多相似之处,但也有不同之处。但是,您可以使用 windows-1252 字符集和编码(其中还包括那些大引号字符)提供完全相同的页面,并且您会看到完全相同的结果。

因此,简而言之,不要将字符编码与字体样式混淆。

顺便说一句,您的 ResponseEncoding="utf-8" 指令是多余的,因为在 web.config 中设置了相同的内容。无论如何,UTF-8 是默认值,因此您甚至可能不需要在 web.config 中使用它。

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.

肤浅与狂妄 2024-08-27 08:14:05

您所看到的是应用于文本区域(多行文本框)和文本框(输入)的不同默认字体。文本区域使用 Courier New,单行文本框使用 Arial。如果您应用将文本框和文本区域的字体系列设置为相同的样式,则您粘贴的文本将匹配。尝试这些,您应该会看到粘贴的内容完全匹配:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文