problem with programmatically created template columns

D

David C

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.
 
T

Teemu Keiski

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).
 
D

David C

Teemu,

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

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top