tags:

views:

34

answers:

3

I have a very simple asp:textbox with the multiline attribute enabled. I then accept just text, with no markup, from the textbox. Is there a common method by which line breaks and returns can be converted to <p> and <br/> tags?

I'm not looking for anything earth shattering, but at the same time I don't just want to do something like:

html.Insert(0, "<p>");
html.Replace(Enviroment.NewLine + Enviroment.NewLine, "</p><p>");
html.Replace(Enviroment.NewLine, "<br/>");
html.Append("</p>");

The above code doesn't work right, as in generating correct html, if there are more than 2 line breaks in a row. Having html like <br/></p><p> is not good; the <br/> can be removed.

+1  A: 

How about throwing it in a <pre> tag. Isn't that what it's there for anyway?

klausbyskov
I don't want to use <pre>.
Mark
Why not? Are you more comfortable re-inventing the wheel?
klausbyskov
I don't want to preserve line-breaks and spaces. I want to convert the text to be sematically correct html.
Mark
+1  A: 

Depending on exactly what you are doing with the content, my typical recommendation is to ONLY use the <br /> syntax, and not to try and handle paragraphs.

Mitchel Sellers
+3  A: 

Your other option is to take the text box contents and instead of trying for line a paragraph breaks just put the text between PRE tags. Like this:

<PRE>
Your text from the text box...

and a line after a break...
</PRE>
Paul Sasik