Re: problem with programmatically created template columns
- From: "Teemu Keiski" <joteke@xxxxxxxxxxxxxxx>
- Date: Wed, 21 Dec 2005 22:06:37 +0200
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" <nospammer@xxxxxxxxxx> wrote in message
news:%23IL1NbmBGHA.3896@xxxxxxxxxxxxxxxxxxxxxxx
>
>
>
> 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 ***
.
- Follow-Ups:
- References:
- problem with programmatically created template columns
- From: David C
- problem with programmatically created template columns
- Prev by Date: Re: shopping cart
- Next by Date: nested datalist problems
- Previous by thread: problem with programmatically created template columns
- Next by thread: Re: problem with programmatically created template columns
- Index(es):
Relevant Pages
|