Convert New Lines to BRs


B

Brad Baker

I'm trying to write a small asp.net 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">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "IEDescription") %> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</ASP:Repeater>

Thanks,
Brad
 
Ad

Advertisements

S

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:

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


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:

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

Hope this helps.

Regards,

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

Advertisements

K

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
formatting
}

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

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

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

Top