keep formatting entered in asp.net textbox (carriage return, new line, etc)

谁说我不能喝 提交于 2019-12-24 07:14:02

问题


I have a textbox using multiline mode and when I create the stored procedure parameter for updating I use Server.HtmlEncode on the textbox value. I can copy the content from the data record into Notepad and it shows the spaces where the user has pressed the Enter key for new paragraphs.

When displaying that content on a page (not a textbox for editing; just assigning the content to a Literal control), it has all the text run together.

I have done some searches about using Replace and different escape sequences or Environment.NewLine. I am still confused about how to have the text display as the user entered it.


回答1:


Assuming you're using C# you can do the following when you display the string on the page

theStringYouWantToFormat.Replace(char.ConvertFromUtf32(13),"<br/>")

When you call Server.HtmlEncode on the value you grab from a text box it'll look at the text and encode any HTML tags contained in that text box so for example <script></script> would be encoded to &lt;script&gt;&lt;/script&gt;.



来源:https://stackoverflow.com/questions/5421009/keep-formatting-entered-in-asp-net-textbox-carriage-return-new-line-etc

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