problem with programmatically created template columns

Discussion in 'ASP .Net' started by David C, Dec 21, 2005.

  1. David C

    David C Guest

    I followed the torial using this from MSDN
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vste
    chart/html/vbtchCreatingWebServerControlTemplatesProgrammatically.asp

    to programmtically create template columns. But the problem is, they
    disappear upon postback.

    1) Is my only choice to rebind the datasource upon every postback?

    2) even if I do rebind, when I have a checkbox embedded on each row, all
    of the checkboxes get unchecked upon every postback.

    Your help will be greatly appreciated.


    *** Sent via Developersdex http://www.developersdex.com ***
    David C, Dec 21, 2005
    #1
    1. Advertising

  2. David C

    Teemu Keiski Guest

    Hi,

    you don't need to bind on every postback, but I'd think you'd need to
    instantiate the templates (into templateColumn(s)) on every request. E.g
    like on the last sample, but not doing filling the data source databinding
    and data-binding on eevry request (but only when needed to refresh)

    I'd separate it like this:

    'Creating the templates
    Private Function CreateTemplates(dg As DataGrid)
    Dim tc1 As New TemplateColumn()
    tc1.HeaderTemplate = New _
    DataGridTemplate(ListItemType.Header, "Column1")
    tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")
    tc1.EditItemTemplate = New _
    DataGridTemplate(ListItemType.EditItem, "Column1")
    tc1.FooterTemplate = New _
    DataGridTemplate(ListItemType.Footer, "Column1")
    dg.Columns.Add(tc1)

    Dim tc2 As New TemplateColumn()
    tc2.HeaderTemplate = New _
    DataGridTemplate(ListItemType.Header, "Column2")
    tc2.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column2")
    tc2.EditItemTemplate = New _
    DataGridTemplate(ListItemType.EditItem, "Column2")
    tc2.FooterTemplate = New _
    DataGridTemplate(ListItemType.Footer, "Column2")
    dg.Columns.Add(tc2)

    End Function

    'binding the grid to a data source
    Private Function BindGrid(dg As DataGrid)
    SqlDataAdapter1.Fill(DsCategories1)
    dg.DataSource = DsCategories1
    dg.DataBind()
    End Function

    Then Page_Load could look like:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Instantiate templates & columns on every request
    CreateTemplates(DataGrid1)

    If Not Page.IsPostBack Then
    'Bind only on initial request
    BindGrid(DataGrid1)
    End if

    End Sub

    Of course, you'd then rebind the grid with BindGrid() when you need to do
    that (when data changes).

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke



    "David C" <> wrote in message
    news:%...
    >
    >
    >
    > I followed the torial using this from MSDN
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vste
    > chart/html/vbtchCreatingWebServerControlTemplatesProgrammatically.asp
    >
    > to programmtically create template columns. But the problem is, they
    > disappear upon postback.
    >
    > 1) Is my only choice to rebind the datasource upon every postback?
    >
    > 2) even if I do rebind, when I have a checkbox embedded on each row, all
    > of the checkboxes get unchecked upon every postback.
    >
    > Your help will be greatly appreciated.
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Teemu Keiski, Dec 21, 2005
    #2
    1. Advertising

  3. David C

    David C Guest

    Teemu,

    It does not work. The template columns still disappear even though I
    recreate the columns on every page load.

    *** Sent via Developersdex http://www.developersdex.com ***
    David C, Dec 21, 2005
    #3
    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. Nicole
    Replies:
    2
    Views:
    4,509
    Nicole
    Nov 5, 2003
  2. =?Utf-8?B?U3VtYQ==?=
    Replies:
    2
    Views:
    506
    =?Utf-8?B?U3VtYQ==?=
    Dec 23, 2004
  3. Luis Esteban Valencia

    Programmatically creating template columns for datagrid

    Luis Esteban Valencia, Jan 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    640
    Saravana
    Jan 11, 2005
  4. =?Utf-8?B?anRlbmNhdGU=?=

    Click Events in programmatically created GridView Template

    =?Utf-8?B?anRlbmNhdGU=?=, Jul 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    794
    =?Utf-8?B?anRlbmNhdGU=?=
    Jul 28, 2006
  5. Replies:
    1
    Views:
    323
    Teemu Keiski
    Apr 17, 2006
Loading...

Share This Page