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: 124244

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

kawish 4/24/2015 7:04 AM
In response to Using The Themes Helper In Razor Web Pages
Nice:)...

Mahdi 4/23/2015 9:49 PM
In response to Adding a New Row In The Razor WebGrid
Thank u very very. its useful for me...

Jerrie Pelser 4/23/2015 4:24 PM
In response to Entity Framework 6 Recipe: Storing And Managing Time
Nice article Mike! I think there is an error in the House property getter of MovieFormModel. Your...

mike 4/22/2015 4:45 PM
In response to Parameter Queries in ASP.NET with MS Access
If the insert statement got failed how w can i get the error message of as string?...

frank 4/22/2015 9:44 AM
In response to Sessions in ASP.NET 5
Mike can you write up an article showcasing the difference / upgrade paths of ASP.net Web Pages and...

Scott 4/21/2015 11:39 PM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
Excellent! Just what I need for the current application I am working on. Even to the extent that I a...

Benjamin 4/21/2015 4:57 AM
In response to Creating a Connection String and Working with SQL Server LocalDB
Hi Mike, great article. I am learning a lot. As commented @Evita, the &lt;Key> Data Annotation is I...

Wayne Hudson 4/20/2015 9:14 PM
In response to Inline Editing With The WebGrid
Thank you very much for your articles. They've helped me a lot. How would you handle inline such...

Dan 4/20/2015 5:01 PM
In response to Sessions in ASP.NET 5
Can I ask how these sort of options are affected by the IIS configuration options when hosted in be...

Shaheen 4/20/2015 3:36 PM
In response to Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Perfect! Saved me from wasting hours....