Convert New Lines to BRs


Brad Baker

I'm trying to write a small webpage in c#. I've written code which
connects to a microsoft sql database, runs a sql query and then prints that
data out using a repeater.

The problem is that the data in the database is in plain text and contains
new lines (\n). When I print the records I need convert new lines to <br>'s.
Is there an easy way to do this?

Here is where I am printing the data.

<ASP:Repeater id="results_repeater" runat="server">
<td><%# DataBinder.Eval(Container.DataItem, "IEDescription") %> </td>




Steven Cheng[MSFT]

Hi Brad,

Welcome to the ASPNET newsgroup.

As for the displaying newline in web page for the text data from database,
I think you can consider using the string.replace method. And the linefeed
in normal text can be represented by the following characters:

"\r\n" (in c#)

So we can just use this function in our databinding expression.e.g:

<asp:Label ID="Label1" runat="server" Text='<%#
"IEDescription").ToString().Replace("\r\n","<br/>") %>'></asp:Label>

Also, you can also define a custom helper function in page's code behind
class and call it in databinding expression to do the work, like:

<asp:Label ID="Label1" runat="server"
Text='<%# CustomFunc(DataBinder.Eval(Container.DataItem, "IEDescription"))

Hope this helps.


Steven Cheng
Microsoft Online Community Support


When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.


This posting is provided "AS IS" with no warranties, and confers no rights.



Kevin Spencer

Create a method in your CodeBehind that takes a string as an argument, and
replaces all line breaks with "<br>" tags, such as the following:

protected string ConvertBr(string s)
return s.Replace("\n", "<br>\n"); // Kept the newline for HTML

Then, in your Repeater, call that function, like so:

<%# ConvertBr(Container.DataItem["IEDescription"]) %>


Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.