Link to home
Start Free TrialLog in
Avatar of melinalt
melinaltFlag for United States of America

asked on

Gridview not rendering html from datatable cell

I am using a datatable to bind a gridview.

The datatable has some cells with HTML code in there (which I am adding dynamically).

HTML links are displayed correctly BUT any other type of controls (input, table, etc) are not being displayed, the grid simply blanks out the cells value.

I do not want to use the rowbound to display the text in the cells because this grid is inside an usercontrol and i want to be able to expose the rowbound event outside of the usercontrol.

How can I get the gridview to display the HTML text in the datatable cell ?


Sub bindGrid()

        'create column dynamically
        Dim newcol As New BoundField
        newcol.HeaderText = "User History"
        newcol.HtmlEncode = "False"
        gvwData.Columns.Add(newcol)

        'create column dynamically
        newcol = New BoundField
        newcol.HeaderText = "User"
        newcol.HtmlEncode = "False"
        gvwData.Columns.Add(newcol)

        Dim tempCol As New TemplateField
        tempCol.HeaderText = "Select"
        gvwData.Columns.Add(tempCol)

        With gvwData
            .EnableViewState = True
            .AutoGenerateColumns = False
            .DataSource = Me.cleanUpData()
            .DataBind()
        End With

    End Sub

    Function cleanUpData() As DataTable


        Dim cleanTable As New DataTable
        Dim newCol As New DataColumn
        newCol = New DataColumn("userid", System.Type.GetType("System.String"))

        newCol = New DataColumn("User", System.Type.GetType("System.String"))
        cleanTable.Columns.Add(newCol)

        newCol = New DataColumn("Select", System.Type.GetType("System.String"))
        cleanTable.Columns.Add(newCol)
       
        'Cretae the new row
        Dim dr As DataRow = cleanTable.NewRow()

        'write a table to be displayed in first column of the grid
        'NOTE: THIS LINK SHOWS UP CORRECTLY
        Dim lnk As New StringBuilder
        lnk.Append("<a href='history.aspx?userid=5'>")
        lnk.Append("View History")
        lnk.Append("</a>")
        dr(0) = HttpUtility.HtmlEncode(lnk.ToString)


        'write a table to be displayed in second column of the grid
        Dim dtls As New StringBuilder
        dtls.Append("<table width='100%'>")
        dtls.Append("<tr>")
        dtls.Append("<td align='left' valign='top' >")
        dtls.Append("Name:")
        dtls.Append("</td>")
        dtls.Append("<td align='left' valign='top' >")
        dtls.Append("Joe")
        dtls.Append("></td>")
        dtls.Append("</tr>")
        dtls.Append("<tr>")
        dtls.Append("<td align='left' valign='top' >")
        dtls.Append("Last Name:")
        dtls.Append("</td>")
        dtls.Append("<td align='left' valign='top' >")
        dtls.Append("Doe")
        dtls.Append("></td>")
        dtls.Append("</tr>")
        dtls.Append("</table>")
        dr(1) = HttpUtility.HtmlEncode(dtls.ToString)



        Dim cbx As New StringBuilder
        cbx.Append("<input type='checkbox' ")
        cbx.Append("name='cbxSelect' ")
        cbx.Append("id='cbxSelect' ")
        cbx.Append("title='Select' ")
        cbx.Append("/>")
        dr(2) = HttpUtility.HtmlEncode(cbx.ToString)


        cleanTable.Rows.Add(dr)


        Return cleanTable

    End Function

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        For i As Integer = 0 To Me.gvwData.Rows.Count - 1
            For c As Integer = 0 To Me.gvwData.Columns.Count - 1
                Dim encoded As String = gvwData.Rows(i).Cells(c).Text 'NOTE:  HERE THE TEXT IS RETURNING EMPTY.. JUST ONE BLANK SPACE
                gvwData.Rows(i).Cells(c).Text() = Context.Server.HtmlDecode(encoded)
            Next
        Next
        MyBase.Render(writer)
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of melinalt

ASKER

You were right.  I was missing the newcol.DataField = "userid".
Thanks