asp.net textbox foriegn character encoding issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tparks69
    New Member
    • Apr 2008
    • 1

    #1

    asp.net textbox foriegn character encoding issue

    I'm getting some Chinese language data from sql server in some encoded format, maybe unicode?... it starts with an ampersand '&', then there is a '#' pound sign, then a 5 digit integer, then a semi colon. I notice it gets translated when I post it exactly, so I'm trying to descibe it here. If I put some spaces between it, a single character looks like this (remove spaces):

    & # 35843 ;

    Anyway, THIS is the big problem:

    1. In ASP.NET 2.0 web page, if I programmaticall y move the data into an asp:textbox, or an html input textbox, I get the data exactly as it looks in SQL server with the ampersand, pound, number, semicolon format. But I need this to show the Chinese characters.

    2. If I move this data into a the innerHtml property of a div tag for example, I see Chinese characters as expected, for example:

    articleArea.Inn erHtml = article.NativeH eadline; //this works great!!!

    4. Interstingly, if I cut and paste from the DIV in step 2, to the textbox, the textbox shows the Chinese characters just fine... but this does not solve the problem of course, just an interesting note.

    5. I did also try changing the font to MS Arial Unicode and that didn't work, also tried changing the encoding in the page directive to UTF-8, no dice

    6. Another interesting note, a gridview column will display the data just fine too!

    7. Is there some conversion I can do in c# server side to get it to work? I know for example, that IF the data is in this format:

    char[] chinese = {'\u6B22','\u8F CE','\u4F7F','\ u7528','\u0020' };

    and I move this to the textbox, the data also shows as expected. But how to get the ampersand, pound, number, semicolon format translated to the above?

    Anyone fix this problem already?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Just curious, is your database table's column a varchar or an nvarchar?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      The &#<character s>; format is the html encoding of unicode characters.

      Comment

      • mldisibio
        Recognized Expert New Member
        • Sep 2008
        • 191

        #4
        Just to expand a bit on Plater's correct answer: when you have extended character sets, they can be encoded as unicode or utf-8, that is, a two or three byte numeric representation.
        When you want to tell a browser to display an extended character, you can use a "numeric character reference" which takes the form of "&" + "#" +the unicode value fo the character + ";"

        Someone already translated your chinese characters into numeric character references and stored them that way in your database.

        When you paste the value of the database field into a "div", the div is expecting pure html, hence it translates your character into its correct display.

        When you programatically fill in the value of a text input, the text input reads the value literally, hence it does not display it correctly.

        You could, for example, programatically load the characters into a hidden input, and then copy the value to the text box, as this very simple example shows: (remove all the spaces in the numeric references first)

        Code:
        <html>
        <body>
        <div>& # 35843;</div>
        <input type="text" value="& # 35843;" id="tb1"/>
        <input type="text" value="" id="tb2"/>
        <input type="hidden" value=" & # 35843;" id="stor"/>
        </body>
        <script language="javascript">
         tb2.value = stor.value;
        </script>
        </html>
        The DIV displays the chinese character;
        The first input box displays a literal copy of the numeric reference;
        The second input box displays the chinese character;

        Comment

        Working...