HTMLencode HTMLdecode
Solution 1:
The simple solution would be to do:
string str1 = Server.HtmlEncode(TextBox1.Text).Replace("\r\n", "<br />");
This is assuming that you only care about getting the right <br />
tags in place. If you want a real formatter you will need a library like Aaronaught suggested.
Solution 2:
That's not what HtmlEncode
and HtmlDecode
do. Not even close.
Those methods are for "escaping" HTML. <
becomes <
, >
becomes >
, and so on. You use these to escape user entered input in order to avoid Cross-Site Scripting attacks and related issues.
If you want to be able to take plain-text input and transform it into HTML, consider a formatting tool like Markdown (I believe that Stack Overflow uses MarkdownSharp).
If all you want are line breaks, you can use text.Replace("\r\n", "<br/>")
, but handling more complex structures like ordered lists is difficult, and there are already existing tools to handle it.
Solution 3:
HTML doesn't recognize \r\n as a line break. Convert them to "p" or "br" tags.
Post a Comment for "HTMLencode HTMLdecode"