URGENT: Can not get dynamically created template column content

Discussion in 'ASP .Net Web Controls' started by Ahmet Gunes, Nov 7, 2003.

  1. Ahmet Gunes

    Ahmet Gunes Guest

    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
    Ahmet Gunes, Nov 7, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manoj Nair
    Replies:
    2
    Views:
    788
    Manoj Nair
    May 20, 2004
  2. Replies:
    5
    Views:
    482
    Eliyahu Goldin
    Sep 3, 2006
  3. msimmons
    Replies:
    0
    Views:
    452
    msimmons
    Jul 16, 2009
  4. Angel
    Replies:
    1
    Views:
    134
    Phillip Williams
    Dec 28, 2005
  5. mortb
    Replies:
    2
    Views:
    141
    Grant Wagner
    Jul 2, 2004
Loading...

Share This Page