Asp. HTML parsing method of user input text in net

Source: Internet
Author: User
Tags string
The Asp.net|asp.net 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 labeled "\ r \ n" and the latter is "<br>". This brings a problem of conversion.

Before doing the conversion, consider a few questions:

1, the textbox with "\ r \ n" 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: "<", "&", "", "<", these are the pages of special significance 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:

"<" à "<"

"&" à "&"

"À" "

...... (the ellipsis below is indicated here)

Step Two:

"À" "

"\ r \ n" à "<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 D

Step Two: a B c D

First step conversion: a B c D

As you can see, after the first conversion has been unable to distinguish between the user input "" and the Conversion of "", in the second conversion will be unified processing error. The results show: a b c 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 = "<a B";

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

Table2 is an HTML control

Table2.rows[0]. Cells[0]. InnerHtml = temp;

HtmlTable has two similar properties "InnerHtml", "InnerText", which describes the differences between the two properties:

InnerHtml: When displayed, HTML parsing is performed on the values passed in, as in the example above.

InnerText: When displayed, the incoming values are not parsed, and the incoming values are displayed directly. For example, the above example is changed to: Table2.rows[0]. Cells[0]. innertext = temp; Then the effect will turn to: <a b

Although these two properties are easy to use and seem to meet the usual usage requirements, innertext has two problems:

1, unable to mark the line break, whether "\ r \ n" or "<br>" is not a newline mark

2, consecutive spaces still show only one space.

So it is not advisable to use innertext directly without converting the string. But conversion is a very tedious process because there are so many special tags in HTML. Thankfully, ASP.net provides the Httpserverutil class, which provides the HTMLEncode () and HtmlDecode () methods, where we use only the HTMLEncode () method. We can convert this way:

"\ r \ n" will be automatically added to the textbox when it is hard to return

String temp = "A b c d\r\ne<";

Server is a property of the page class, which means that any ASPX page can be used in the codebehind

Because ASPX pages are inherited from the page class.

temp = Server.HTMLEncode (temp);

temp = temp. Replace ("", "");

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 D

e<

Here you may think of a problem, what if the user input "\ r \ n" How to do? Don't worry, the designer of the Web component has taken this into account, because the user input "\ r \ n" will be automatically converted to "\\r\\n" (before we do the conversion, of course).



Accomplished:)



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.