"Reprint" ASP. NET GridView Chinese text content cannot be wrapped (line wrapping/Normal line wrapping)

Source: Internet
Author: User

ASP. NET GridView Text content cannot wrap (line wrap/normal wrap) font: [Increase decrease] type: Reprint with GridView to display the curriculum, each cell contains the course name, class location, teacher name, and then I want them to display the branch, Interested friends can understand, may be helpful to you

When I was working on a project recently, I encountered a problem: I use the GridView to display the curriculum, each cell contains the course name, the class location, the teacher's name, and then I want them to be displayed in a branch, with the following effects:

However, the GridView is too tenacious, whether I splicing strings with "\ r \ n" or "<br/>", the results are useless, are not displayed on the branch. After nearly half a day of time to inquire about the information, finally solved the problem, the following to share with you.

The problem of line wrapping in the GridView can be divided into two categories: one is automatic wrapping, the other is normal line wrapping.

First Class: Word Wrapping

The GridView default is line wrap, which means that the GridView automatically wraps when the displayed string is longer.

For example, we want to show: "When the string is longer, it will wrap the line," the effect is as follows:

Of course, if we don't want it to wrap, add the following code in the background of the page:

Copy CodeThe code is as follows:
Normal line break
GRIDVIEW1.ATTRIBUTES.ADD ("Style", "word-break:keep-all;word-wrap:normal");


Add the following:

Here's the second category: normal line breaks .

The key to normal line break is the selection of newline characters, some people say "\ r \ n" with the escape character, and some people say "<br/>". Which one is it? Let's take a look at the two types of displays:

1, display the string: "When the string" + "\ r \ n" + "long \ r \ n Time will automatically wrap the line."

Shown below (and without adding the same):

2. Display string: "When the string is longer" + "<br/>" + "Here's the line."

Shown below:

As you can see, adding a newline character alone does not solve the problem. The correct workaround is as follows:

1, should use "<br/>"

2.
① If you bind the field to set the template column, then the corresponding BoundField setting parameter Htmlencode= "false".

② If field is automatically generated: Add GridView1_RowDataBound Event

Copy CodeThe code is as follows:
<summary>
So that the contents of the GridView can be wrapped.
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Gridview1_rowdatabound1 (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
Tablecellcollection cells = e.row.cells;
foreach (TableCell cell in cells)
{
Cell. Text = Server.htmldecode (cell. Text); Note: All of the columns in the HTML code are output in HTML format, and if only one of those columns needs to be converted, a small modification is required here.
}
}
}


Look at the results after adding:

Display string: "When the string" + "<br/>" + "longer time" + "<br/>" + "Here's the line."

Display effect:

The problem is solved as soon as the

ends.

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.