DB Text with NewLines, etc. for display in a Repeater or DataGrid

G

Guest

If I have text data from a database which will contain NewLine characters
because it was entered from a Multi-Line TextBox, and now I want to display
the output in a Repeater or other DataBound List Control, how can I set it up
so that it'll properly show the text with the paragraph breaks, etc?

At first, I thought I'd just have to write a routine that did
s = s.Replace(Environment.Newline, "<br/>");
and that works fine for showing simple, one-off text. But if I want to put
this in a Repeater, how can I run my "Replace" function in-line in the
ItemTemplate of the repeater? Can I? If so, can you show me the syntax for
how to do that? Is this the bext way to do it?

Thanks.

Alex
 
S

Steven Cheng[MSFT]

Hi Alex,

Here it is (assume you're using ASP.NET 2.0):

================
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<br /><hr /><br />
CategoryName:<%# Eval("CategoryName") %>
<br />
CategoryName:<%#
Eval("Description").ToString().Replace(Environment.NewLine,"<br/>") %>
</ItemTemplate>
</asp:Repeater>
==================

Hope this helps.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

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.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
D

digitaljeebus

or you could always wrap it in <pre></pre> tags, that way it maintains
all formatting (if there's extra spaces or tab characters or something
like that)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top