4

I have an input field on my html page where the user can enter Unicode text, for example i enter : ыва ыва ыва ыва ыва ыв

When the form is posted, i check the value posted and it is posted as : ыва ыва ыва ыва

The content type of the page is set as :Content-Type: text/html; charset=utf-8

When i display the posted value on the webpage, it shows as ыва ыва ыва ыва instead of ыва ыва ыва ыва ыва ыв.

How can i fix this to display properly? Do i need to do convert the encoding ? I believe c# strings by default are utf8, and my html page charset is also set as utf-8 - so not sure what's going on.

Update: Here's my ASP Page :

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="utf8.aspx.cs" Inherits="enterprise10._garbage.utf8"
    ValidateRequest="false" Theme="" EnableTheming="false" ResponseEncoding="utf-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">    
    <title></title>
</head>
<body>
    <form action="" method="post" runat="server">
    <asp:TextBox ID="UserInputText" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="UserInputLabel" runat="server" Text="Label"></asp:Label>
    <br />

       <input type="submit" />

    <hr />

<b>    Sample Text displays correctly on the page : </b><br />
    ыва ыва ыва ыва ыва ыв  



    </form>
</body>
</html>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
            UserInputLabel.Text = UserInputText.Text;
}

Screen Output (IE9) Before form post:

enter image description here

Screen Out after form post

enter image description here

3
  • 1
    .NET strings are effectively "Unicode" by default, although the storage is actually UTF-16. When you write the data out though, it should obey whatever encoding you've set. Please post some code. Commented May 2, 2011 at 19:05
  • 2
    This really helped me when I had encoding problems: joelonsoftware.com/articles/Unicode.html Commented May 2, 2011 at 19:08
  • +1 @fredw yes i've read that particular post by Joel before, its a good read. Commented May 2, 2011 at 19:10

1 Answer 1

10

Adjust the encoding of your application, in the web.config file set the desired encoding for requests and response (under <system.web>)

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
Sign up to request clarification or add additional context in comments.

7 Comments

@BrunoLM i do have the encoding setup explicitly in the page, see my update.
Ok i found the problem, like BrunoLM said in my web.config there was an existing entry for globalization set to "windows-1252" . I removed that and it works fine now.
Is there a way to explicitly override the requestEncoding in the code behind, irrespective for the web.config entry ?
@ace you missed the requestEncoding, I guess the value is not being sent correctly to the server side. Try adding RequestEncoding="utf-8" on your page...
You can not have RequestEncoding on the ASPX page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.