Search code examples
asp.net.netvb.netweb-controls

Extending the BoundField class to add <span> tag inside <td> tag rendered by gridview


I would like to add a property to the BoundField control that will change rendering as show below.

<td>My Bound Text></td>

Should become

<td><span>My Bound Text</span></td>

This is all I have so far...

Public Class BoundField
    Inherits System.Web.UI.WebControls.BoundField

    Dim _ellipse As Boolean
    Public Property Ellipse() As Boolean
        Get
            Return _ellipse
        End Get
        Set(value As Boolean)
            _ellipse = value
        End Set
    End Property

End Class

I clarify my goal... I have the same problem as this poster, however I am using a .net gridview and would like render my html to use the fix offered here.

Using CSS to create table cells of a specific width with no word wrapping

EDIT I think I may be going about this all wrong, maybe you don't change the rendering by extending the BoundField, but rather add the properties to the boundfield then change the way the gridview renders its fields based on the properties of those fields.

Which also leads me down the path of control adapters, if anyone can confirm or invalidate this suspicion I would appreciate it.


Solution

  • Not the solution I set out looking for, but it does the job. Note I still extend the bound field to add the ellipse property as shown above. In addition I extended the gridview to override the OnRowBound event as shown below.

        Protected Overrides Sub OnRowDataBound(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        For i As Integer = 0 To MyBase.Columns.Count - 1
            If CType(MyBase.Columns(i), BoundField).Ellipse = True Then
                e.Row.Cells(i).Text = "<div class=""ellipsis"">" & e.Row.Cells(i).Text & "</div>"
            End If
        Next
    End Sub
    

    I'm am still open to a better implementation, but I'm going with this for now so I can move forward.