Categories
asp.net

GridView vs DataGrid, Server.HtmlDecode()

dotNet 2.0后,M$开始推荐使用GridView取代DataGrid(Comparing the GridView and DataGrid Web Server Controls)。前些天在做Testlink测试Case的Chart,然后想在表中插入HTML format的时候出现了奇怪的问题。DataGrid一切正常,GridView貌似对直接插入的HTML进行了encode,HTML被原原本本地显示了出来。 DataView Encoded 在MSDN上找到Server类下边有个HTML解码的方法Server.HtmlDecode(),题外话还有个Server.HtmlEncode()的方法。

protected void gvTestPlan_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);
  }
}

这下加入的HTML format就不会给encode了。 DataView Decoded

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.