C# 如何在 GridView 单元格中的(即 <br>)中呈现解码的 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/431840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:08:38  来源:igfitidea点击:

How to render decoded HTML in a (i.e. a <br>) in GridView cell

c#asp.netgridviewnewline

提问by core

I'm binding a GridView to an LINQ query. Some of the fields in the objects created by the LINQ statement are strings, and need to contain new lines.

我将 GridView 绑定到 LINQ 查询。LINQ 语句创建的对象中的一些字段是字符串,需要包含新行。

Apparently, GridView HTML-encodes everything in each cell, so I can't insert a <br /> to create a new line within a cell.

显然,GridView HTML 对每个单元格中的所有内容进行编码,因此我无法插入 <br /> 以在单元格内创建新行。

How do I tell GridView not to HTML encode the contents of cells?

如何告诉 GridView 不要对单元格的内容进行 HTML 编码?

Maybe I should use a different control instead?

也许我应该使用不同的控件?

采纳答案by Ray Booysen

Can you subscribe to the RowDataBound event? If you can, you can run:

您可以订阅 RowDataBound 事件吗?如果可以,您可以运行:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;
}

回答by configurator

Are normal newlines preserved in output? If so, you can send the newlines, and use the css style white-space: pre, which would preserve newlines, spaces and tabs.

输出中是否保留了正常的换行符?如果是这样,您可以发送换行符,并使用 css style white-space: pre,这将保留换行符、空格和制表符。

回答by DaamonTurne

I got around this by first inserting the data into my sql-server table from a multi-line textbox using

我通过首先使用多行文本框将数据插入到我的 sql-server 表中来解决这个问题

   replace (txt = Replace(txt, vbCrLf,"<br />"))

Then I used Ray Booysen's solution to return it to my gridview:

然后我使用 Ray Booysen 的解决方案将其返回到我的 gridview:

 Protected Sub grdHist_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdHist.RowDataBound

      Dim col1 As String = HttpUtility.HtmlDecode(e.Row.Cells(2).Text)

      e.Row.Cells(2).Text = col1

End Sub

回答by John

Booysen's answer works but only for one column. If you run a loop in the RowDataBound event, you can substitute a variable for the [0] and have this work on each column if you want. Here's what I did:

Booysen 的答案有效,但仅适用于一列。如果您在 RowDataBound 事件中运行一个循环,您可以用一个变量代替 [0] 并根据需要在每一列上进行这项工作。这是我所做的:

protected void gridCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 1; i < 4; i++)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decode = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
            e.Row.Cells[i].Text = decode;
        }
    }
}

Mine is deliberately started at 1 because of my data, but obviously it will work with whatever you need.

由于我的数据,我故意从 1 开始,但显然它可以与您需要的任何东西配合使用。

回答by Aaron Daniels

What about setting the HtmlEncodepropertyto false? To me, this is much simpler.

HtmlEncode属性设置为false? 对我来说,这要简​​单得多。

<asp:BoundField DataField="MyColumn" HtmlEncode="False" />

回答by Developer

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
            e.Row.Cells[i].Text = decodedText;
        }
    }
}

回答by Hank Brandenburg

protected void gvHead_OnRowDataBound(object sender, GridViewRowEventArgs e) {
  for (int i = 0; i < e.Row.Cells.Count; i++) 
    e.Row.Cells[i].Text = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
}

回答by szuuuken

You have to bind to the the DataBoundGrid event and change the Rendering for the columns you want to render HTML code.

您必须绑定到 DataBoundGrid 事件并更改要呈现 HTML 代码的列的呈现。

public event EventHandler DataBoundGrid {
  add { ctlOverviewGridView.DataBound += value; }
  remove { ctlOverviewGridView.DataBound -= value; }
}

ctlOverview.DataBoundGrid += (sender, args) => {
  ((sender as ASPxGridView).Columns["YourColumnName"] as GridViewDataTextColumn).PropertiesTextEdit.EncodeHtml = false;
};

回答by Reza Paidar

@Ray Booysenanswers is right but in some cases HtmlDecode() can't handle your problem. you can use UrlDecode() instead of HtmlDecode().
here is another solution:

@Ray Booysen答案是正确的,但在某些情况下 HtmlDecode() 无法处理您的问题。您可以使用 UrlDecode() 而不是 HtmlDecode()。
这是另一种解决方案:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string decodedText = HttpUtility.UrlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;
}