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

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

- pankaj

very nice

i'm use this in my code

thank you....




- phani

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

@phani,

You should put single quotes around the Text property:

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

- Anil Sharma

Great Sir

Recent Comments

sandeep 8/28/2015 7:03 AM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
very good article, i like it........keep writing such quality article in future. thx Mike....

Hassan, MVC Learner 8/28/2015 6:37 AM
In response to Get The Drop On ASP.NET MVC DropDownLists
Great Help, simple, great and patiently explained article !...

Anvesh 8/28/2015 12:39 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
what if we are taking postback values from FormCollections instead of an array int[] category. How...

Ben 8/27/2015 10:50 PM
In response to Simple File Download Protection with ASP.NET
Is it possible to setup your project to publish files to outside of your root directory? I would to...

Fred 8/26/2015 12:50 AM
In response to WebMatrix Opens Wrong Version Of Visual Studio
I enjoyed many of your tutorials but the problem is none of the tutorials are combined like most be....

Muhammad Ashikuzzaman 8/25/2015 2:48 PM
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
That's a very good tips for razor...

Sergey 8/25/2015 8:32 AM
In response to More Flexible Routing For ASP.NET Web Pages
Hi. How I can set up my site to get urldata from link for default page? site.com/default/1-...

Tony Gray 8/25/2015 6:27 AM
In response to Adding Validation
Hi Mike, Really helpful article and series. Thanks. Small typo in 4th paragraph you have so...

amanda n 8/25/2015 12:38 AM
In response to Solving the Operation Must Use An Updateable Query error
Thank you very much. I'm a uni student and while solving coding problems is usually enjoyable and me...

salman 8/23/2015 9:25 AM
In response to How To Send Email In ASP.NET MVC
thanks ......