Prevent HTML encoding in auto-generated GridView columns

前端 未结 7 2117
渐次进展
渐次进展 2020-11-27 06:46

I have a GridView bound to a DataTable that I construct. Most columns in the table contain the raw HTML for a hypelinklink, and I would like that HTML to render as a link i

7条回答
  •  感情败类
    2020-11-27 07:31

    Since all the answers seem to be in C# and the questions was not specific, I ran into this issue using ASP.Net and VB.Net and the accepted solution did not work for me in VB (though I imagine it does work in C#). Hopefully this helps anyone working with VB.Net in ASP who stumbles upon this as I have.

    In VB.Net BoundColumn cannot be added to Gridview.Columns as it is not a System.Web.UI.WebControls.DataControlField so instead one must use a BoundField which is a DataControlField.

    BoundColoumn also does not have an HtmlEncode property however BoundField does. Also, in VB.Net DataSource becomes DataField.

            For Each dataCol As DataColumn In dv.Table.Columns
                Dim boundCol As New BoundField With {
                    .DataField = dataCol.ColumnName,
                    .HeaderText = dataCol.ColumnName,
                    .HtmlEncode = False
                }
    
                gvResult.Columns.Add(boundCol)
            Next
    
            gvResult.DataSource = dv
            gvResult.Databind()
    

    Also note that you must explicitly set AutoGenerateColumns="False" or the GridView will still generate columns along with the columns added above.

提交回复
热议问题