views:

5780

answers:

11

Hi,

What is the way of seperating Line-Breaks and Space in MultiLine Textbox or Html Text are while inserting database(Asp.Net).Forexample what should i do if i want to insert

Hello

World

to database with line breaks?And Also While displaying what should i do too?i dont want to use Editor for it. Thanks for ur Help

A: 

I always store user input as it is provided, but then you must always be very careful when displaying it back again. Line breaks are simply a case of replacing System.Environment.Newline with "<br />". Not sure what the best practice is for preserving other white space when rendered as html, would be interesting to hear what other people do.

Dan Powley
+6  A: 

The line breaks should persist to the db. When you retreive the info and display as Html just replace the line break (i.e. Environment.NewLine) with a line break tag (i.e. < b r / > )

deadbug
Don't forget to Html.Encode() first, then do the Replace() if it's user submitted content.
Dzejms
You ought to use HtmlEncode() *always*, whether the content is user-enetered or not. All it takes is one accidentally inserted `<` or `'` to really mess things up.
Loadmaster
A: 

Retaining white space after rendering to HTML is most easily done by converting it into the HTML entity &nbsp;.

Grayside
A: 

But the problem is how to seperate line break and space.Now when i insert

Hello

World

or hello world,it is saved with same character in db.(Space Character).Therefore i cant seperate them and i am showing hellow world although user uses line breaks such as above.The value,which comes from TextBox or html area,are the same as with line break or space.

A: 

You need to replace all instances of vbcrlf (environment.newline) with the html tag [< b r >]

And to allow multiple spaces you need to replace space with [& n b s p ;]

That will convert your plain test to html "enough" to be viewed as html (works for me on the entered text sections of my web site)

From my tags it's only the text between the [] and remove the spaces ... placing the tag text into the page caused it to be displayed as the html !!!!

spacemonkeys
A: 

Blockquote

A: 

hey i tried chr(13)chr(10) , didnt work, even tried /n , didnt work and /r/n didnt work either..it just prints the thing i write, doesnt give a line break in the textbor ( multi lines )

A: 

You can use &#10;

Chad Smith
No, you can't. Spaces are (mostly) ignored in HTML. He needs to use an actual formatting line-break, which is `<br/>`.
Loadmaster
You're right. I thought he was asking about adding line breaks to the textarea.
Chad Smith
A: 

The only suggestion above that worked for me was: <& # 1 0 ;> I am using a text box generated by Front Page now running on a Linux server.

Multiple uses of the string will not create multiple line breaks, but a single entry of the string on a new line provides a double line break suitable for a new paragraph.

Before moving my page from a server using a Windows OS, I was able to accomplish Line Breaks by double "Enters."

Old Web Guy

Old Web Guy
+2  A: 
Loadmaster
A: 

< br /> didnt work for me.

\r\n did.

Jazz