Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net C# New line

Tags:

c#

asp.net


I need help with making a line break. I have this code:

<asp:Repeater runat="server" ID="rptPosts">
    <ItemTemplate>
        <h2><%#Server.HtmlEncode(Eval("post_title").ToString()) %></h2>
        <em><%#Server.HtmlEncode(Eval("post_posted").ToString()) %></em>
        <br />
        <%#Server.HtmlEncode(Eval("post_content").ToString()) %>
    </ItemTemplate>
</asp:Repeater>

I want to make post_content generate automatic new lines. I've tried with environment.newline but I can't seem to get it to work.

EDIT:
I mean to generate HTML breaks. Like nl2br() in PHP.

like image 995
Petter Pettersson Avatar asked Jan 16 '14 12:01

Petter Pettersson


People also ask

What is ASP.NET C?

ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services. The name stands for Active Server Pages Network Enabled Technologies. ASP.NET (software)

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

What is ASP.NET C# used for?

ASP.NET is a server-side technology used for developing dynamic websites and web applications. ASP.NET aids developers to create web applications by using HTML, CSS, and JavaScript. ASP.NET is the latest version of Active Server Pages, which Microsoft developed to build websites.

Can I learn ASP.NET without C#?

Yes, you can learn them both at the same time, it is often easier to start if you know C# or VB beforehand, but not a requirement at all to be successful. There are many places to start, but 4GuysFromRolla.com is a great tutorial site.


1 Answers

You need to do it after you have HTML encoded it. Otherwise any HTML you put in will end up getting encoded. So your best bet is:

Server.HtmlEncode(Eval("post_content").ToString())
    .Replace(Environment.NewLine, "<br>"+Environment.NewLine)

This will encode everything as normal and then everywhere it finds a new line it will replace it with a <br> tag (line break in HTML) and then the new line again (to make sure you keep the plain text line breaks as well as the HTML ones.

This isn't making "post_content generate automatic new lines" but I'm not too clear on what that means since post_content is just a key in whatever your data item is. If this is not adequate you'll have to clarify your requirements a bit more.

like image 107
Chris Avatar answered Oct 06 '22 12:10

Chris