[RESOLVED] Binding a GridView programmatically with datatable containing html tags
let's say we have datatable with "<a>12</a>" as rows.
when i try to bind it to gridview programmatically
I'm not getting href links like 12 But just a strings like "<a>12</a>"
Code:
Dim datatable as new DataTable
datatable.Columns.Add("No")
dim datarow1 ad DataRow = datatable.newrow()
datarow1(0) = "<a>12</a>"
datatable.rows.add(datarow1)
gridview1.DataSource = datatable
gridview1.DataBind()
Re: Binding a GridView programmatically with datatable containing html tags
On the grid column set HtmlEncode="false". By default it's true so <> tags will be encoded and appear as text.
Re: Binding a GridView programmatically with datatable containing html tags
Thanks For the reply. I solved the problem. Problem i had was columns generated automatically and there was no way to change properties.
Following is the snippet i have used.
Code:
Protected Sub GridDun_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridDun.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Text = Server.HtmlDecode(e.Row.Cells(i).Text)
Next
End If
End Sub
Re: [RESOLVED] Binding a GridView programmatically with datatable containing html tag
It seams strange that there is no way to set that property when auto generating columns. I couldn't find any.
Re: [RESOLVED] Binding a GridView programmatically with datatable containing html tag
Depending on where the contents of the cells are coming from, i.e. from user input, it is good practice to always HtmlEncode and HtmlDecode so that you don't get any unexpected side effects from displays the content on your page.
Gary