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

3.66 (61 votes)

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


- 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


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

- Mike


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


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

- 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


You need to use Html.Raw:

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

- pietro

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


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:

- kevin

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

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

<div style="white-space: pre-wrap">@MyMemoField</div>

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


- pankaj

very nice

i'm use this in my code

thank you....

- phani

I tried to put the below line in my code in my Textbox properties in C#.
<asp:TextBox ID="txtRS" runat="server" Text="<%# Eval("MyMultiLineValue").ToString().Replace(\r\n,"<br />") %>"></asp:TextBox>

When I build the code , its giving the error, The server tag is not well formed.
what can be the issue. can you please guide me.

- Mike


You should put single quotes around the Text property:

Text='<%# Eval("MyMultiLineValue").ToString().Replace(\r\n,"<br />") %>'

- Anil Sharma

Great Sir

Recent Comments

Rajasekar 24/11/2015 12:27
In response to Import Data From Excel to Access with ASP.NET
While use this code i'm facing on error: "Unrecognized Database format C:\mydabase.accdb" can any...

Parmod 24/11/2015 07:28
In response to ASP.NET 5 Project Basics
For a new learner (Fresher) in ASP.NET there is a issue Fresher have to learn two types of , old...

Robert 22/11/2015 21:35
In response to ASP.NET 5 By Numbers
I have to agree fully with Paul, this does sound like an entire mis-mash of technologies. Sort of in...

Christian 21/11/2015 15:46
In response to MVC 5 with EF 6 in Visual Basic - Creating an Entity Framework Data Model
Many thanks Mike to introduce me in the EF6 Code First way of thinking. Exactly what I need for my...

ax plains 20/11/2015 16:29
In response to Examining the Details and Delete Methods
Hello, really great tutorial for a beginner like me! Is it possible to have an explanation on how a...

Abdul Latif 20/11/2015 14:42
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Could anyone please help me, I am getting : "OfficeOpenXml.ExcelPackage" does not contain a for...

Thomas 20/11/2015 09:44
In response to Cheat Sheet - .NET Framework Exceptions
Hey Mike, nice list. I've also checked your article on how you created the list, but is there any to...

Pramod Gagare 19/11/2015 11:18
In response to Date Formatting in C#

Robby 19/11/2015 08:16
In response to WebMatrix - Database Helpers for IN Clauses
Would it also be possible to override the default query and querysingle methods to include the azure...

Menja 18/11/2015 08:28
In response to Sessions and Shopping Carts
Thank you for a perfect description and that you show all the screenshot at the same time!!! It a...