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:
Last Updated:
Posted by:
Total Views to date: 117663

23 Comments

- 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.


- David

Thank you very much.

- Rajashree

It's fantastic.

Thanks you.

- andy

THANKS!

- Suranga

Thank you. It works :)

- 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!

- 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,"
")

- Mike

@foo

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

- 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!

- 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 />") %>' />

- Manishdhaduk

Thank you very very very much

- parabellum

Ingenious solution. Thank you for sharing.

- reader

Perfect. Thanks.

- Clever Moon

Ya da man

- sam

thanks - works a treat

- 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, "
);

- 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

- Mike

@ben

You need to use Html.Raw:

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

- 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

- 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

- kevin

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

- 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

- 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 I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to forums.asp.net instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

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

Recent Comments

hosein ey 2/25/2015 1:56 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
tnx for this article do you think next version of entityframework support's the sql server and ?...

Saywer Ford 2/25/2015 5:15 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Great Article. I did everything right and working fine. How about page specific js files. Lets I...

Saravanan 2/24/2015 10:54 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Hi, Great article about the Bundling and minification. Regards the caching as you mentioned the...

Justin 2/24/2015 10:43 AM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
I'm having the same problem as Ingmar. Everything is working fine locally in Visual Studio, but I it...

Logan Mudlo 2/23/2015 4:59 PM
In response to WebMatrix - A First Application
Is there a way to prevent the automatic close on a Database.Open() call?...

Mog0 2/23/2015 11:16 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Probably a silly question but why did you disable cascade delete and implement it yourself?...

Jose-Maria 2/19/2015 3:29 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
Just great ! Many thanks Mike....

Satyabrata Mohapatra 2/19/2015 12:02 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
This is great!!...

Ryan Helmoski 2/19/2015 10:24 AM
In response to Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Thank you!...

Harsha 2/19/2015 8:27 AM
In response to iTextSharp - Adding Text with Chunks, Phrases and Paragraphs
Hi, Can text area be created so that I can type text in the area in pdf document? We have Add text...