Skip to content Skip to sidebar Skip to footer

Response.Write Print Text From Database As HTML

I'm storing text from a textarea to my database. This text can contain new lines, special characters etc. However I want to make sure that when I show it using response.write, it c

Solution 1:

Never use "\n" always use Environment.NewLine, instead:

foreach (DataRow row in dview.Table.Rows)
{
    Response.Write("<tr>"); 
    Response.Write(String.Format("<td>{0}<br/><br/></td>", row[1].ToString().Replace(Environment.NewLine, "<br/>")));
    Response.Write("</tr>");
}

Solution 2:

Don't do it that way, you'll run into problems, especially when quotes and symbols are involved.

Store the data like this :

 HttpUtility.HtmlEncode("<p>Lorem ipsum ...</p>"); //your HTML to encode goes here

And retrieve it like this:

myPlaceHolder.Text = HttpUtility.HTMLDecode(myData);

Here's a little more information on HTMLEncode/Decode


Solution 3:

You could display the output inside a <pre></pre> block which will preserve all whitespace, including multiple spaces, newlines, etc.

HTML encoding is recommended so any angle brackets in the data don't break the output HTML.


Post a Comment for "Response.Write Print Text From Database As HTML"