2007.12.28

Nedtælling til nytår 2008

Kategori: Sjov og ballade — Michael Schøler, Kl. 14:14:00

2007.12.26

Glædelig jul & Godt nytår

Kategori: Tvillingerne — Michael Schøler, Kl. 09:57:15

Glædelig jul og Godt nytår fra os alle.

2005 2006 2007

Klik på et julekort for at få det vist i stor udgave (kræver login til fotoalbum).

2007.12.11

UpdatePanel UTF-8 encoding woes

Kategori: Programmering — Michael Schøler, Kl. 19:06:00

Encoding frustrations...
The last couple of days I have been struggling with text encoding issues in a C# web project, and in the process I have found an “official Microsoft hack” that might be of interest to others as well - hence this article. Using hacks to solve a problem is not best practice and should for all sakes be avoided but alas it is the only solution if you find yourself stuck to developing your web project in Microsoft Visual Studio 2005 so read on in that spirit. Note: Microsoft has fixed the bug and expects us all to upgrade to Visual Studio 2008 - they won’t fix 2005. Sure, everybody can change their development environment from day to day.

I was pulling hair out at an exponentially escalating rate, almost rendering me bald, trying to figure out what the “#!” happened when submitting text values from a standard C# aspx form. The web application I was working on had due to project specifications been forced to use ISO-8859-1 encoding by setting the globalization property in the web.config file and by content-type specifications both in the page declaration and meta tag information in the page’s head section but texts were in a seemingly sporadic way arriving at the server as default UTF-8 encoded text?!

By following the traffic between the server and the client browser, using the Fiddler2 debugging proxy, I could see that text values got transformed from ISO-8859-1 encoding (standard latin-1 charset) into UTF-8 (Unicode Transformation Format) in the POST request. That suggested the client browser was responsible for the errornous encoding. So, I tried IE6, IE7, FireFox, Opera - and they all failed. Clearly the problem had to be somewhere else than in the browser - it is simply not possible for Microsoft, Mozilla and Opera to be in such uniform agreement.

Googling the issue led down many blind paths, as it was unclear exactly what was the cause of the error, and it was therefore difficult to figure out the “magic” search terms to use.

So, I ended up meticulously trimming down the webpage one tag and item at a time - and finally I had managed to locate the bug: Microsoft. Now all that remains is to locate the responsible employee and then activate a Microsoft W.S.Y.P.tm punishment of an adequate magnitude.

The Microsoft ASP.NET AJAX Extensions framework wrongly treats all text strings as UTF-8, regardless of the specified encoding of the web application. This means that by using partial updates in an UpdatePanel the ajax framework gets busy talking jibberish to your web application server and you end up spending countless hours wondering about what the heck goes on.

You can try the following simple code example to see it for yourself (ASP .NET AJAX enabled C# website project), or skip it and read on.

Enter this in your web.config file in the <system.web> section:

<globalization requestEncoding=”iso-8859-1″ responseEncoding=”iso-8859-1″/>

TestEncoding.aspx

  1. <%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“TestEncoding.aspx.cs” ValidateRequest=“false” ContentType=“text/html; charset=iso-8859-1″ Inherits=“TestEncoding” %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns=“http://www.w3.org/1999/xhtml”>
  4.   <meta http-equiv=“Content-Type” content=“text/html; charset=iso-8859-1″ />
  5.   <title>Test encoding</title>
  6. </head>
  7.   <form id=“Main” runat=“server”>
  8.     <asp:ScriptManager ID=“ScriptManager” runat=“server” EnablePartialRendering=“true”></asp:ScriptManager>
  9.                
  10.     <h1>ISO-8859-1 / UTF-8 encoding problem in UpdatePanel</h1>
  11.                
  12.     Reguar submit button:<br />
  13.     <input type=“submit” value=“Submit” /><br />
  14.     <hr />
  15.                
  16.     Submit button in an UpdatePanel:<br />
  17.     <asp:UpdatePanel ID=“UpdatePanel3″ runat=“server” RenderMode=“inline”>
  18.       <ContentTemplate>
  19.         <input type=“submit” value=“Submit” />
  20.       </ContentTemplate>
  21.     </asp:UpdatePanel><br />
  22.     <hr />
  23.  
  24.     Result from server:<br />
  25.     <asp:UpdatePanel ID=“UpdatePanel2″ runat=“server” RenderMode=“inline”>
  26.       <ContentTemplate>
  27.         <asp:Label ID=“Result” Text=“” runat=“server” />
  28.       </ContentTemplate>
  29.     </asp:UpdatePanel><br />
  30.     <hr />
  31.  
  32.     String value to send to the server:<br />
  33.     <asp:TextBox ID=“TextBoxGroupName” Text=“æøå” runat=“server”></asp:TextBox> (do not modify!)
  34.   </form>
  35. </body>
  36. </html>

TestEncoding.aspx.cs

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Xml;
  12. using System.Xml.Xsl;
  13. using System.IO;
  14. using System.Text;
  15.  
  16. public partial class TestEncoding : System.Web.UI.Page
  17. {
  18.   protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.       if (IsPostBack)
  21.       {
  22.       string t = TextBoxGroupName.Text;
  23.       int l = t.Length;
  24.       if (l == 6)
  25.       {
  26.         Result.Text = “<b style=’color:red’>Error: Got an UTF-8 server reply: ‘” + t + “‘.</b>”;
  27.       }
  28.       else
  29.       {
  30.         Result.Text = “<b style=’color:#060;’>Approved: Got an ISO-8859-1 server reply: ‘” + t + “‘.</b>”;
  31.       }
  32.     }
  33.     else
  34.     {
  35.       Result.Text = “<i>Press one of the submit buttons.</i>”;
  36.     }
  37.   }
  38. }

When you run this example pressing the regular submit button will result in the server receiving the expected string value “æøå”. Pressing the submit button inside the UpdatePanel however, posts the UTF-8 encoded string value “æøå” to the server.

Google + hard earned knowledge to the rescue

Now googling payed off - and I was able to locate this official bug report from Microsoft regarding non UTF-8 encoding and UpdatePanel.

The solution is to insert the following javascript on all form pages in your web application where the problem could potentially occur:

The bug solving hack

  1. <script type=“text/javascript”>
  2. <!–
  3. function pageLoad(sender, args) {
  4.   if (!args.get_isPartialLoad()) {
  5.     Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnBeginRequest);
  6.   }
  7. }
  8. function OnBeginRequest(sender, args) {
  9.   args.get_request().get_headers()[“Content-Type”] = “application/x-www-form-urlencoded; charset=utf-8″;
  10. }
  11. //–>
  12. </script>

Throw that piece of code into the head section of the TestEncoding.aspx file, and voilá, the value is interpretted correctly by the Microsoft .NET AJAX framework.

I sure hope my article will save some of your time, as I am all to clearly aware of how much time I have lost in the process of tracking down the problem. Maybe I even ought to invoice the Redmond company.

2007.12.10

CVI-Projects

Kategori: Brok, Programmering — Michael Schøler, Kl. 21:03:42

Christian Effenberger has made a collection of quite awesome effect libraries which can be found on his CVI-Projects page, CVI being an acronym for Canvas, Vml and Image effects.

I’ll get back to how cool I think his effect libraries are in a moment but first off, I have to derail this articles focus and ponder a bit about his websites front page. In the moment of writing I think it’s rubbish. It currently states:

“This page is temporarily closed in protest against software patents. Websites may soon be closed down regularly due to software patents. Software patents can get you prosecuted for publishing texts you wrote yourself!”

Apparently Christian is frustrated about software patents, and links to a page where you can read more about how to support the cause - his front page no doubt beign a copy/paste template from that “get-more-information-page”. 

As I understand his quarrels he is opposed to software patents and supports this by closing down access to his site. Well, his effect libraries are still there freely available and quite public, and they are very, very cool - I promise to get back to it right away - so to me his digital demonstration seems a bit half-hearted and indifferent. Furthermore, I find it troublesome that the site promoting this war against software patents is violating its own advice. Close down your sites, don’t write texts, you’ll get prosecuted - Danger Will Robinson, Danger!

Oh well I got no beef with software patents, as long as I’m not getting sued I guess. Back to the nice effects!

The CVI-Projects page has a number of unobtrusive javascript effects you can use for your website - free of charge if private or non-commercial, licensed if not:

  • Curl
  • Reflex
  • Edge
  • Corner
  • Glossy
  • Instant
  • Slided
  • Filmed
  • Louped

Especially I like the loupe effect. It illustrates the raw power of scripted effects I think.

The scripted effects are accomplished by utilizing the browsers graphical capabilities to the maximum. The effects libraries nicely degrades according to the supported graphic functions provided by the users browser. If none are present an unaltered image with no effects are shown.

Take a look at the various effects for yourself. Enjoy.