We help IT Professionals succeed at work.

Gridview not rendering html from datatable cell

melinalt
melinalt asked
on
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

Comment
Watch Question

Commented:
I can further your issue a little simply by assigning datafields to your dynamically created boundfields and following your previous model to assign the 3rd column (checkbox).

Also correcting an error in your cleanupdata that wasnt generating the proper number of columns for your table.rows.add at the bottom.

Doing all this will generate the grid and html elements you are looking for.

Creating a templatefield would be a much more complex issue. If you want to do that you'll have to do a search on dyncamically creating templatefields and build up from that.

I don't think this is necessarily a good idea overall but maybe I just dont have enough information to follow your scope/direction.
Sub bindGrid()

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

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

        newcol = New BoundField
        newcol.HeaderText = "Select"
        newcol.HtmlEncode = "False"
        newcol.DataField = "Select"
        gvwData.Columns.Add(newcol)

        'dday
        'This is more complex than the above two and will require much more work
        '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"))
        cleanTable.Columns.Add(newCol)

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

        newCol = New DataColumn("Select", System.Type.GetType("System.String"))
        'DDAY had to add this to prevent error
        'Although its useless without further implementation of the templatefield
        'or using another type like asp:buttonfield
        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

Open in new window

Author

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