Asp. HTML parsing method of user input text in net

Source: Internet
Author: User
The Web page lets the user enter text in a TextBox, then stores it in the database, and then reads it out of the database and displays it on the page. It's often a problem to do this, because the textbox is actually a Windows component, and Web page display tags such as:<p>,<td>,<div>, and so on, the parsing method of the character is different. For example, the former line is marked as "" and the latter is "<br>". This brings a problem of conversion.
Before doing the conversion, consider a few questions:
1, the TextBox with "" To mark the line break and the page with "<br>" Mark
2, the continuous space in the Web page as a space processing, such as "a B C" will show "a B C"
3, user input special characters such as: "<", "&", "&nbsp;", "&lt;", these are the pages have special meaning characters, will be resolved, and the purpose of the user input is of course not want to be resolved.
To solve these problems now, one solution is to display them in a textbox, so that you do not have to do any conversion, as long as the textbox.readonly set to true, so that to some extent to meet the requirements. But often for the beauty of the web, it is not advisable to do so. The better way to do this is to parse the string that the user entered:
First step:
"<" à "&lt;"
"&" à "&amp;"
"&nbsp;" à "&amp;nbsp;"
...... (the ellipsis below is indicated here)
Step Two:
"" À "&nbsp;"
"" À "<br>"
This must be done in two steps, because if you first make a second conversion, mix the special strings entered by the user with the special strings that are converted, such as:
User input: A b c&nbsp;d
Second Step conversion: A&nbsp;b&nbsp;&nbsp;&nbsp;c&nbsp;d
First Step conversion: A&amp;nbsp;b&amp;nbsp;&amp;nbsp;&amp;nbsp;c&amp;nbsp;d
As you can see, after the first conversion, you can't tell "&nbsp;" about user input. and the conversion of the "&nbsp;", when doing a second conversion will be unified processing will be wrong. Results show: A&nbsp;b&nbsp;&nbsp;&nbsp;c&nbsp;d
If you strictly follow the first step, the second step in the conversion sequence to convert the problem will not occur.
The converted string is stored in the database, and the string read from the database is assigned to the HTML component when it is displayed, for example:
This assumes that temp is read from the database, and this demo table and htmltable other controls are similar
The effect shown is: <a b

Table1 is a Web control
The String temp = "&lt;a&nbsp;b";
Table1.rows[0]. Cells[0]. Text = temp;
Related Article

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.