469,887 Members | 1,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

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 programmatically 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.InnerHtml = article.NativeHeadline; //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','\u8FCE','\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?
Sep 29 '08 #1
3 4657
Curtis Rutland
3,256 Expert 2GB
Just curious, is your database table's column a varchar or an nvarchar?
Sep 29 '08 #2
Plater
7,872 Expert 4TB
The &#<characters>; format is the html encoding of unicode characters.
Sep 29 '08 #3
mldisibio
190 Expert 100+
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)

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <div>& # 35843;</div>
  4. <input type="text" value="& # 35843;" id="tb1"/>
  5. <input type="text" value="" id="tb2"/>
  6. <input type="hidden" value=" & # 35843;" id="stor"/>
  7. </body>
  8. <script language="javascript">
  9.  tb2.value = stor.value;
  10. </script>
  11. </html>
  12.  
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;
Sep 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Safalra | last post: by
8 posts views Thread by Brand Bogard | last post: by
44 posts views Thread by Kulgan | last post: by
1 post views Thread by PHP Wooer | last post: by
10 posts views Thread by Martin | last post: by
13 posts views Thread by =?Utf-8?B?YXVsZGg=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.