Re: problem with programmatically created template columns



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 ***


.



Relevant Pages

  • Re: Certificate Version Question
    ... PKCS #10 request and also submit that request to a CA. ... > to create machine certificates for Win2000 clients using a Win2003 ... > wizard for creating a request file for an SSL cert? ... >>> certificate templates for which I can make a request. ...
    (microsoft.public.win2000.security)
  • Re: Absence Request Template Workflow
    ... > After downloading some of the new MS WSS Templates and following the ... the Employee Absence Request and Vacation Schedule ... > I've managed to get the InfoPath form to correctly submit, ... But I'm missing the connection to the Vacation Schedule ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Computer Cerrtificate
    ... enterprise mode always uses templates. ... > I am running enterprize certificate services on win2k3 server standard ... I need to request computer certificate. ...
    (microsoft.public.windows.server.security)
  • Re: Problem to enable SSL Communication Over LDAP For W2k Domain Controllers
    ... Is the CA configured to issue any templates? ... > Certificate Request Settings' as stated in step 5 of the article. ... 'Recovery Agents for Crypted Data'. ...
    (microsoft.public.win2000.security)