Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > problem with programmatically created template columns

Reply
Thread Tools

problem with programmatically created template columns

 
 
David C
Guest
Posts: n/a
 
      12-21-2005



I followed the torial using this from MSDN
http://msdn.microsoft.com/library/de.../en-us/dv_vste
chart/html/vbtchCreatingWebServerControlTemplatesProgrammatic ally.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 ***
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      12-21-2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
>
>
> I followed the torial using this from MSDN
> http://msdn.microsoft.com/library/de.../en-us/dv_vste
> chart/html/vbtchCreatingWebServerControlTemplatesProgrammatic ally.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 ***



 
Reply With Quote
 
 
 
 
David C
Guest
Posts: n/a
 
      12-21-2005
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 ***
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Click Events in programmatically created GridView Template =?Utf-8?B?anRlbmNhdGU=?= ASP .Net 0 07-28-2006 08:35 PM
Events not firing in datagrid with programmatically created columns J.ShepherdII@gmail.com ASP .Net Datagrid Control 1 04-17-2006 02:27 PM
Programmatically creating template columns for datagrid Luis Esteban Valencia ASP .Net 1 01-11-2005 02:46 PM
Problem with dynamically created template columns and updatecomman =?Utf-8?B?U3VtYQ==?= ASP .Net 2 12-23-2004 10:29 AM
Programmatically Created DataGrid Template Columns Nicole ASP .Net 2 11-05-2003 12:52 AM



Advertisments