CraigWardman.com

[ Textbox CrLf in Firefox using AJAX UpdatePanel ]

/Blog/Post

Textbox CrLf in Firefox using AJAX UpdatePanel

When using a MultiLine textbox inside an ASP.NET AJAX update panel, you may encounter problems with carriage return line feeds in your text on the server using Firefox (and potentially other browsers).

Internet Explorer uses the Windows style CrLf (13 10) for newlines in a textarea but Firefox uses Unix style Lf (10) only.

On a synchronous postback it seems ASP.NET fixes this and you will get CrLf in your text on the server. However, when you are posting back asynchronously using AJAX, you only get Lf in your text when Firefox is used.

In order to clean this up and have consistant data, I wrote a simple regex replace to make sure all Lf are preceded by a Cr.

public static string CleanUnixCrLf(string textIn)
{
   //firefox only uses Lf and not CrLf
   return System.Text.RegularExpressions.Regex.Replace(textIn, "([^\r])[\n]", "$1\r\n");
}

Share This!

3 Comments to Textbox CrLf in Firefox using AJAX UpdatePanel

  1. October 26, 2010 at 12:49 am | Permalink

    Very helpful, thanks. This had me scratching my head for a bit. I’ve asked a question of Stack Overflow to see if anyone knows of other ways to resolve this issue.

    http://stackoverflow.com/questions/4019928/carriage-return-ascii-chr-13-is-missing-from-textbox-postback-values-when-used

  2. Morgan's Gravatar Morgan
    February 18, 2010 at 11:17 am | Permalink

    Hello,

    Just noticed this a week ago… Ajax layer is still perfectible…

    Bye and continue the good job.

    Bye

    Morgan (French Asp.Net Dev)

Leave a Reply

CraigWardman.com
Car Leasing | Lease Cars