The webpage uses TextBox to allow users to input text, store it to the database, and read the text displayed on the page from the database. In this case, you will often encounter many problems, because TextBox is actually a Windows component, and the webpage display labels such as: <p>, <td>, <div>, etc, the methods for parsing characters are different. For example, the line feed of the former is marked as "", and the latter is "<br> ". This leads to a conversion problem.
Before performing the conversion, consider the following:
1. TextBox uses "" to mark line breaks and the webpage uses "<br>" to mark
2. Consecutive spaces in the webpage are processed as one space. For example, "a B c" will display "a B c"
3. Users enter special characters such as "<", "&", "& nbsp;", and "& lt;", which are special characters in the webpage, will be parsed, and the purpose of user input is of course not to be parsed.
To solve these problems, one solution is to use TextBox for display during display, so that no conversion is required, as long as TextBox is set. set ReadOnly to true to meet the requirements to a certain extent. However, this is often not desirable for the appearance of webpages. A better way is to parse the user input string and perform the conversion as follows:
Step 1:
"<" À "& lt ;"
"&" À "& amp ;"
"& Nbsp;" à "& amp; nbsp ;"
...... (The ellipsis is described below)
Step 2:
"" À "& nbsp ;"
"" À "<br>"
Here, the conversion must be completed in two steps, because if you first perform the second step of conversion, you can mix the special strings you enter with the special strings that are converted, for example:
User input: a B c & nbsp; d
Step 2 conversion: a & nbsp; B & nbsp; c & nbsp; d
Step 1: a & amp; nbsp; B & amp; nbsp; c & amp; nbsp; d
We can see that after the first conversion, the "& nbsp;" entered by the user and the converted "& nbsp;" cannot be distinguished ;", an error occurs when the second conversion is performed. Result: a & nbsp; B & nbsp; c & nbsp; d
If you strictly follow the first step, the second step of the conversion sequence will not encounter this problem.
Store the converted string to the database, and assign a value to the HTML component when displaying the string read from the database. For example:
// Assume that temp is read from the database. Here, the Table and other HtmlTable controls are similar.
// The displayed result is: <a B
// Table1 is a Web Control
String temp = "& lt; a & nbsp; B ";
Table1.Rows [0]. Cells [0]. Text = temp;
// Table2 is an HTML control.
Table2.Rows [0]. Cells [0]. InnerHtml = temp;
There are two pages in this news. Currently, there are two pages in page 1st.