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


- 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

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

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


- 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

adam 05/10/2015 14:35
In response to Integrating Web API with ASP.NET Razor Web Pages
Can you re-open this web api project in webmatrix, once you've added web api? Basically I'm looking...

nish 24/09/2015 18:48
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
Very Interresting stuff! it really helped me to send an int value by checking a checkbox!...

Uğur Dinç 24/09/2015 16:45
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Simplest and best explanation on Quartz.NET. Thank you!...

woo 24/09/2015 15:34
In response to Implementing Google's EU End User Consent Policy
Is there any way for the banner to appear only to EU visitors? I am referring to the jQuery code...

Justin 24/09/2015 11:10
In response to Using ASP.NET Identity with Razor Web Pages
Hi Mike, Very helpful article again, thanks. One query that I'm trying to work out is how you to...

hb 23/09/2015 23:12
In response to WebMatrix Opens Wrong Version Of Visual Studio
Mike - I got this working when I went decided to look at the community edition of VS2015 and tried I...

Muneer 22/09/2015 14:55
In response to Scheduled Tasks In ASP.NET With Quartz.Net
I have an error with these two is not recognizing the commands. am i missing any library? I am web...

David 22/09/2015 13:57
In response to iTextSharp - Working with Fonts
Mike your articles about itextsharp are excellent, really helpful and well written. In the future...

Peter 22/09/2015 06:39
In response to Accessing Your Model's Data from a Controller
Thanks.Got it. ...

sreedhar kandukuri 21/09/2015 14:05
In response to Integrating Web API with ASP.NET Razor Web Pages
Nice Overview...