We help IT Professionals succeed at work.
Get Started

Gridview not rendering html from datatable cell

melinalt
melinalt asked
on
1,280 Views
Last Modified: 2012-05-12
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:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE