keep formatting entered in asp.net textbox (carria

2019-09-03 19:37发布

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条回答
乱世女痞
2楼-- · 2019-09-03 19:45

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;.

查看更多
登录 后发表回答