How to retain carriage returns or line breaks in an ASP.NET web page

Many newcomers to web development cannot get their head around why the carriage returns they made in their data on input from a textarea, or from a text file, Excel spreadsheet etc. do not appear when the web page renders.

The solution is fairly obvious once the newcomer realises that a web page is only the browser's interpretation of html markup, and that a new line in html is represented by the <br /> tag. So what is needed is a way to swap carriage returns or line feeds with the <br /> tag. Well, a way to Replace() them, actually.

<%# Eval("MyMultiLineValue").ToString().Replace(<linebreak>,"<br />") %>

The string.Replace() method allows this, but we also need to identify what we want to replace with the html tag. How is a new line <linebreak> represented in C# or VB.Net?

In C#, it's "\r\n", while in VB.Net, it's vbcrlf. However, there is also a language independent option that does just the same thing: Environment.NewLine.

<%# Eval("MyMultiLineValue").ToString().Replace(Environment.NewLine,"<br />") %>

Date Posted: Friday, April 6, 2007 7:55 PM
Last Updated: Wednesday, May 30, 2007 10:13 AM
Posted by: Mikesdotnetting
Total Views to date: 89784

23 Comments

Thursday, January 8, 2009 12:36 AM - Jack

Thanks for this article! it helped me a lot. I think i'll start coming here a lot earlier during my problem solving from now on.


Monday, March 30, 2009 1:06 AM - David

Thank you very much.

Saturday, April 18, 2009 3:57 PM - Rajashree

It's fantastic.

Thanks you.

Friday, June 19, 2009 6:00 PM - andy

THANKS!

Friday, July 10, 2009 11:42 PM - Suranga

Thank you. It works :)

Friday, July 24, 2009 11:13 AM - Michael

Thanks a million!

I've been using plugin's like tinymce or a customize TextArea just to replace the line breaks in a TextArea.

I didn't know or ask enough about Environment.NewLine...great!

Thursday, September 3, 2009 3:01 PM - foo

I keep getting the following error:

Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/admin.asp, line 39
Eval(content).ToString().Replace(Environment.NewLine,"
")

Friday, September 4, 2009 6:30 AM - Mike

@foo

You get that error because you are not using ASP.NET. You are using classic ASP. They are totally different.

Thursday, October 1, 2009 2:16 PM - newbieweb

hey mike, actually i'm new to web and id like to know where can i exactly put your suggested code? i'm using detailsview and i wanted the data from sql to appear with linebreaks.. is it in a code behind or within the asp control? im using 3.5 by the way.. thanks in advance!

Friday, October 2, 2009 11:19 AM - Mike

@newbieweb

You would put it in the aspx page as part of the control markup eg:

<asp:Label runat="server" ID="MyLabel" Text='<%# Eval("MyMultiLineValue").ToString().Replace(<linebreak>,"<br />") %>' />

Saturday, November 21, 2009 12:57 PM - Manishdhaduk

Thank you very very very much

Wednesday, January 27, 2010 10:00 PM - parabellum

Ingenious solution. Thank you for sharing.

Friday, February 5, 2010 7:17 PM - reader

Perfect. Thanks.

Friday, March 19, 2010 11:02 AM - Clever Moon

Ya da man

Wednesday, March 21, 2012 2:49 PM - sam

thanks - works a treat

Monday, December 10, 2012 5:45 AM - Patrick

Great example. In my case, I am using Regular Expression and the Environment.NewLine worked well in that too (i.e. Regex.Replace(string, Environment.NewLine, "
);

Tuesday, January 15, 2013 10:39 AM - ben

hello, i'm using webpages and trying to retrieve the data from the database like this:

@row.Description.ToString().Replace(Environment.NewLine,"<br />")

but it is physically replacing a new line with the letters &lt;br /&gt;

is there a quick solution to fix this? thanks

Thursday, January 17, 2013 7:57 AM - Mike

@ben

You need to use Html.Raw:

@Html.Raw(row.Description.ToString().Replace("\n", "<br />"))

Thursday, February 28, 2013 9:54 AM - pietro

Hi,
this is not the right way to do it. Html has the right tag to accomplish the same result without using replace of linebreak with BR. Simply use html <pre> tag

Thursday, February 28, 2013 1:44 PM - Mike

@pietro

The <pre> tag is not the correct tag for simple line breaks. It is for preserving all whitespace including tabs, spaces and line breaks. I also defaults to a monospace font. The correct HTML tag for a simple line break is <br />

Check the specs: http://www.w3.org/TR/html401/struct/text.html#line-breaks

Monday, February 24, 2014 2:34 PM - kevin

Nice work. Thia stuff has solved a day's crisis.
Thanks

Sunday, April 13, 2014 8:26 PM - Cláudio Costa

Hi Mike, ben and Pietro

To retrieve data from the database and retain carriage returns, line breaks (and white-spaces), use CSS "white-space" attribute". No code, no HTML.Raw, no tricks.

Ex in Razor:

@MyMemoField

Thursday, August 14, 2014 4:20 PM - Eric

Just wanted to post to say THANK YOU. This finally got it working for my program.

However, I found that this does NOT work if the Eval statement is enclosed in a div block. For some reason, the line breaks do not work with div tags. I used textarea instead, and it worked perfectly.

THANK YOU!
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and some may not be published. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Not relevant to the article
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam
  • Anything in a language I don't understand including gibberish.

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.