URGENT: Can not get dynamically created template column content

A

Ahmet Gunes

As a requirement of our application each time different type and count of columns may be displayed to the user in a datagrid.
For this, I created a template by implementing ITemplate interface and added columns to the grid dynamically.
There is no problem with the display. But when I post back my grid gives Item.Count =0 and Columns.Count = 0.
Where did those items and columns go?

Please help,

Thanks in advance,


I create the columns with the following code:

gct = New GridColumnTemplate(ListItemType.Item, dv(i).Item("COLUMN_NAME"))
col = New TemplateColumn
col.ItemTemplate = gct
col.HeaderText = dv(i).Item("COLUMN_DESCRIPTION")
col.Visible = True
grdTranslator.Columns.Add(col)

And here is my template class:

Friend Class GridColumnTemplate
Implements ITemplate

Dim templateType As ListItemType
Dim columnName As String

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim lc As New Literal
Select Case templateType
Case ListItemType.Header
lc.Text = "<B>" & columnName & "</B>"
container.Controls.Add(lc)
Case ListItemType.Item
Dim lbl As New Label
lbl.Text = ""
AddHandler lbl.DataBinding, AddressOf TemplateControl_DataBinding
container.Controls.Add(lbl)
Case ListItemType.EditItem
Dim tb As New TextBox
tb.Text = ""
tb.ID = "txt" & columnName
tb.EnableViewState = False
AddHandler tb.DataBinding, AddressOf TemplateControl_DataBinding
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
End Sub

Private Sub TemplateControl_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Select Case templateType
Case ListItemType.Header
Case ListItemType.Item
'Dim lc As Literal
'lc = CType(sender, Literal)
'Dim container As DataGridItem
'container = CType(lc.NamingContainer, DataGridItem)
'lc.Text = DataBinder.Eval(container.DataItem, columnName)
Dim lc As Label
lc = CType(sender, Label)
Dim container As DataGridItem
container = CType(lc.NamingContainer, DataGridItem)
lc.Text &= DataBinder.Eval(container.DataItem, columnName)
Case ListItemType.EditItem
Dim tb As TextBox
tb = CType(sender, TextBox)
Dim container As DataGridItem
container = CType(tb.NamingContainer, DataGridItem)
tb.Text &= DataBinder.Eval(container.DataItem, columnName)
Case ListItemType.Footer
End Select
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,020
Latest member
anuradha

Latest Threads

Top