HTML parsing of input text in ASP. NET

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.