When you use the AJAX controls on a page with non-english characters, after a callback has been fired those characters are being messed up. This happens because the XmlHttpRequest javascript object, which is responsible for the callback request, does not support encoding different than UTF-8. We highly recommend that you use UTF-8 encoding for your applications. This can be done by adding the following line into the application's web.config file:
| web.config |
Copy Code |
|
<globalization requestEncoding="UTF-8" fileEncoding="UTF-8" responseEncoding="UTF-8"/> |
However if it is still messes up the page, you can use a function to convert the encoding from the client to the server. Set the encoding to the one you are going to use:
| web.config |
Copy Code |
|
<globalization requestEncoding="iso-8859-1" fileEncoding="iso-8859-1" responseEncoding="iso-8859-1"/> |
and use a custom function similar to the one below:
| C# |
Copy Code |
|
string ConvertText(string srcEncoding, string destEncoding, string textToConvert) { Encoding src = Encoding.GetEncoding(srcEncoding); Encoding dest = Encoding.GetEncoding(destEncoding); byte[] srcTextBytes = src.GetBytes(textToConvert); byte[] destTextBytes = Encoding.Convert(dest, src, srcTextBytes); char[] destChars = new char[src.GetCharCount(destTextBytes, 0, destTextBytes.Length)]; src.GetChars(destTextBytes, 0, destTextBytes.Length, destChars, 0); return new string(destChars); } |
| VB.NET |
Copy Code |
|
Function ConvertText(ByVal srcEncoding As String, ByVal destEncoding As String, ByVal textToConvert As String) As String Dim src As Encoding = Encoding.GetEncoding(srcEncoding) Dim dest As Encoding = Encoding.GetEncoding(destEncoding) Dim srcTextBytes As Byte() = src.GetBytes(textToConvert) Dim destTextBytes As Byte() = Encoding.Convert(dest, src, srcTextBytes) Dim destChars(src.GetCharCount(destTextBytes, 0, destTextBytes.Length)) As Char src.GetChars(destTextBytes, 0, destTextBytes.Length, destChars, 0) Return New String(destChars) End Function |
For example converting from "iso-8859-1" (Swedish) to "UTF-8" will look like this:
lblAjax.Text = "AJAX returned: " + ConvertText("iso-8859-1", "utf-8", tbAjax.Text);