HTML parsing of user input text in ASP. NET

Source: Internet
Author: User
Web Application Textbox Let the user input the text, save it to the database, and read the text displayed on the page from the database. This often causes many problems, because Textbox Is actually Windows Components, and Web page display tags such: <P>, <TD>, <div> . For example, the line feed of the former is marked as" "R" n ", While the latter is" <Br> ". This leads to a conversion problem.

Before performing the conversion, consider the following:

1,TextboxUse""R" n"To mark the line feed while the webpage uses"<Br>Mark

2,Consecutive spaces in the webpage are processed as one space, for example,A B C"Will display"A B C"

3. you can enter special characters such: " " , " & " , " & nbsp; " , " & lt; ", which are special characters in the webpage. Is parsed, and the purpose of user input is certainly not to be parsed.

To solve these problems, one solution is to useTextboxIn this way, no conversion is required.Textbox. readonlySetTrueTo 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;"

""R" n" À"<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; & nbsp; C & nbsp; D

Step 1: A & amp; nbsp; B & amp; nbsp; & amp; nbsp; C & amp; nbsp; d

We can see that after the first conversion, the"& Nbsp;"And the converted"& Nbsp;", Errors will occur 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 directly display the string read from the databaseHtmlComponent assignment, such:

//Assume thatTempIt is read from the database. It is demonstrated here.TableAndHtmltableOther controls are similar

//The result is as follows:<A B

 

// Table1YesWebWidget

String temp = "& lt; A & nbsp; B ";

Table1.rows [0]. cells [0]. Text = temp;

// Table2YesHtmlWidget

Table2.rows [0]. cells [0]. innerhtml = temp;

HtmltableThere are two similar properties"Innerhtml","Innertext"To introduce the differences between the two attributes:

Innerhtml:The input value is displayed.HtmlResolution, just like the example above.

InnErtext:When displayed, it does not parse the new passed value and displays the passed value directly. For example, change the example above:Table2.rows [0]. cells [0]. innertext = temp;The displayed result will be:& Lt; A & nbsp; B

Although these two attributes are easy to use, they seem to be able to meet the needs of ordinary usersInnertextThere are two problems:

1,Line breaks cannot be marked."R" n"Or"<Br>"Are not line breaks.

2,Only one space is displayed for consecutive spaces.

You can directly use innertext is not optional. However, conversion is a very tedious process, because HTML contains many special tags. Fortunately, Asp. net provides httpserverutil class, which provides htmlencode () and htmldecode () method. Here we only use htmlencode () method. We can convert it like this:

//""R" n"InTextboxIt will be automatically added when you press Enter.

String temp = "a B C & nbsp; D" R "ne <";

// server Yes page an attribute of a class, that is, any aspx codebehind All are available

//BecauseAspxAll pages are inheritedPageClass.

Temp = server. htmlencode (temp );

Temp = temp. Replace ("", "& nbsp ;");

Temp = temp. Replace ("R" N "," <br> ");

Table1.rows [0]. cells [0]. Text = temp;

Table2.rows [0]. cells [0]. innerhtml = temp;

//Display Effect:A B C & nbsp; d

// E <

here you may think of a problem, in case the user enters" " R "n " What should I do? Don't worry, the Web component designer has taken this issue into consideration, because the user entered " " R "n " is automatically converted to " " "R" "n ".

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.