Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

Maybe I should use a different control instead?

like image 624
core Avatar asked Jan 10 '09 21:01

core


3 Answers

What about setting the HtmlEncode property to false? To me, this is much simpler.

<asp:BoundField DataField="MyColumn" HtmlEncode="False" />
like image 63
Aaron Daniels Avatar answered Nov 19 '22 06:11

Aaron Daniels


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

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;
}
like image 22
Ray Booysen Avatar answered Nov 19 '22 07:11

Ray Booysen


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;
        }
    }
}
like image 4
Developer Avatar answered Nov 19 '22 08:11

Developer