{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Storing HTML in database and displaying it in a web browser
Summary
Storing HTML in the database with all the HTML tags is straight-forward; however if the entered text has some tags (either HTML or XML) that are to be displayed as they are (i.e. not interpreted as formatting tags), you will run into some trouble with the browser. This article discusses a couple of options in dealing with these issues.
 
Table of Contents
 

The example code needs to be displayed on a web page as it is. For example, if I want the less than symbol to be displayed as it is in the following code fragment:

<b>use b tags to bold the statement</b>

Following should actually be bold: 

use b tags to bold the statement

If you look at the source, you will notice that I am using &lt; to represent < in the first case. In the second case, I actually used the less than symbol. An interesting thing to note is that you do not have to replace the greater than symbol with &gt;. Just change the less than symbol, you will be fine. Similarly, for the following line of code appear with less than and greater than symbols in tact, all you need to do is to replace the less than symbol with &lt;

<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

HtmlEncode and HtmlDecode

If you pass a string through HtmlEncode, it will change all these special HTML symbols into their codes (i.e. < will become &lt;). You can store that in the database and when you obtain the string back to display in a browser, it will render that string with < and > as symbols, not as formatting tags.

If you know you are letting the user enter only text (and no formatting), the above method works perfectly. Meaning, run the user entered string through HtmlEncode and store it in the database and when you retrieve it, pass the string without modifications to the browser (i.e. do not run HtmlDecode). This way, the less than and greater than will not cause any problems in the browser.

If your user entered text is formatted (i.e. it has HTML tags that are to be interpreted as formatting tags) and it could potentially contain < (for example, in code), you need to replace just those symbols. If you are using a simple text box, one way is to ask the user to manually enter &lt; for < and use the HTML tags where the formatting is needed.

Bookmark and Share This

More Articles With Similar Tags
This article talks about various aspects of creating an SQLite database. It also uses Android SQLite shell as an example.
icon-android-shell-main-method.jpg
This article talks about the SQLite packages available in Android SDK.
You have defined a stored procedure that takes parameters. You have created an SqlDataSource that uses these parameters. Now how do you change these parameters at run-time from C# (or VB.Net) code?
This quick snippet shows how to get data using SqlDataSource and attach it to a Repeater to display that data.
About  Contact  Privacy Policy  Site Map