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?
What about setting the HtmlEncode
property to false
? To me, this is much simpler.
<asp:BoundField DataField="MyColumn" HtmlEncode="False" />
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;
}
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With