ASP.Net Text with LineBreak from Multi-Line-TextBox to save in a database

对着背影说爱祢 提交于 2019-11-26 14:38:40

问题


I have PrivateMessage-System in my new Community-Page.

I have a Multiline-Textbox for the Text of a private message. I save this text in a string, this string in a ntext SQL-Coloumn. I read this text from the ntext SQL-Coloum in a string and show it in a label.

When I have 5 linebreaks with the "Enter"-Key in my Multi-line-textbox, the linebreaks will disappeared in the label.

I don't know where they gone lost and what I am doing wrong. any idea?


回答1:


First of all is it actually saving the line breaks to your table? If so, what does it save them as, eg \r\n or CRLF or what?

Your label outputs html, so the only thing that will create a break is a <br /> tag. So you need to find and replace whatever is saved in the database:

Label1.Text = someDatabaseText.Replace("\r\n", "<br />");

Or even better, do the .Replace() before you save it to the database:

someDatabaseField = TextBox1.Text.Replace("\r\n", "<br />");



回答2:


this also works:

lbl_PostContent.Text = lbl_PostContent.Text.Replace(vbCrLf, "<br />")



回答3:


You can solve this with CSS. Assign this CSS to your label:

white-space: pre-wrap;



来源:https://stackoverflow.com/questions/4883613/asp-net-text-with-linebreak-from-multi-line-textbox-to-save-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!