As I said before, the GridView column consists from the set
of Template classes (Header, Item, Alternate and Footer). And in our case we
need to implement two template classes (Header and Item templates).
Look at listing 8; I will use these two template classes in
the constructor of the template field class, and we will create an object from
that template field class.
Listing 8
'Header Template Class
NotInheritable Class MyHeaderTemplate
Implements ITemplate
Sub InstantiateIn(ByVal owner As Control) Implements ITemplate.InstantiateIn
Dim chkboxHeader As New CheckBox
chkboxHeader.ID = "ChkHeader"
owner.Controls.Add(chkboxHeader)
End Sub
End Class
'Item Template Class
NotInheritable Class MyItemTemplate
Implements ITemplate
Sub InstantiateIn(ByVal owner As Control) Implements ITemplate.InstantiateIn
Dim chkboxItem As New CheckBox
chkboxItem.ID = "ChkItem"
owner.Controls.Add(chkboxItem)
End Sub
End Class
Here we are setting the look and feel of the header and item
template field for the checkbox column; as you can see we are setting the
vertical and horizontal align for the header and item template.
Listing 9
Class MyTemplateField
Inherits TemplateField
Private _owner As TemplateOwner
Private _header As MyHeaderTemplate
Private _item As MyItemTemplate
Public Sub New()
_owner = New TemplateOwner
_header = New MyHeaderTemplate
_item = New MyItemTemplate
_header.InstantiateIn(_owner)
Me.HeaderTemplate = _header
_item.InstantiateIn(_owner)
Me.ItemTemplate = _item
Me.HeaderStyle.VerticalAlign = VerticalAlign.Middle
Me.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
Me.HeaderStyle.Width = Unit.Percentage(3)
Me.ItemStyle.VerticalAlign = VerticalAlign.Middle
Me.ItemStyle.HorizontalAlign = HorizontalAlign.Center
End Sub
End Class
<ToolboxItem(False)> _
Public Class TemplateOwner
Inherits WebControl
End Class