Hi,
I have a multipurpose page that display all sorts of data sources in a WebDataGrid.
The grid has an unbound dynamically added WebDataGridCheckBox column added via a TemplateDataField in code. The rest of the columns are created by adding BoundDataField objects to the Columns collection (nothing is defined declarative)
All works well, including paging, until I change the data source.
When switching data sources, I first call ClearDataSource and then Columns.Clear before recreating the unbound and then bound columns.
The new grid renders fine, however when paging, the Checkbox control disappears (although the header is still there).
If I do not call Columns.Clear the problem does not happen, however if the data sources have different columns in them, this causes a problem.
Can anyone help with this?
Thanks
Code Snippets:
Public Function Reset() As Boolean With Grid .ClearDataSource() .Columns.Clear() If Not IsNothing(.Behaviors.Sorting) Then Behaviors.Sorting.SortedColumns.Clear() If Not IsNothing(.Behaviors.Filtering) Then Behaviors.Filtering.ClearBehaviorColumnInfo() BuildColumns() End With Return True End Function
Private Function BuildColumns() As Boolean Dim chkSel = Grid.Columns.FromKey("chkSel") If chkSel Is Nothing Then Dim chk As New WebDataGridCheckbox("chkSel") Dim tdf As New TemplateDataField(False) tdf.ItemTemplate = chk tdf.Key = chk.Key tdf.Width = New Unit(30) tdf.Hidden = Not ShowCheckbox Grid.Columns.Add(tdf) End If Return BuildDataColumns() End Function
Hello JamesKydd ,
Thank you for posting in our forum.
Are you clearing the grid’s columns over the ajax callbacks initiated by the paging as well?
Generally you should clear them only when changing the data source.
Also could you let me know in which event of the page lifecycle you’re clearing the data source and columns and recreating them?
Generally it would be best to be done early in the lifecycle for example in Page Init.
I’m looking forward to your reply.
Best Regards,
Maya Kirova
Developer Support Engineer
Infragistics, Inc.
http://ko.infragistics.com/support
Hi Maya,
Thanks for the reply.
The columns are cleared only in response to the user clicking on a non-grid control (usually a menu item). I believe this always generates a full postback (not AJAX).
The grid is actually in a User Control so the page life-cycle looks something like this:
First Load:
Page renders correctlyUser elects to change grid content
Page Postback (non-ajax):
Page renders correctlyUser elects to page the grid
Ajax Page Postback:
Page renders without the Checkbox column, but no Checkboxes visible.
Having traced through the code now to confirm the above, I think the problem lies with the need to manually recreate the Declaritive column after calling Columns.Clear().
I think the code that generates this column is not working. It looks like this:
Dim chkSel = Grid.Columns.FromKey("chkSel") If chkSel Is Nothing Then Dim chk As New WebDataGridCheckbox("chkSel") Dim tdf As New TemplateDataField(False) tdf.ItemTemplate = chk tdf.Key = chk.Key tdf.Width = New Unit(30) tdf.Hidden = Not ShowCheckbox Grid.Columns.Add(tdf) End If
And the WebDataGridCheckbox class, looks like this:
Public Class WebDataGridCheckbox Implements ITemplate Private _Key As String Public ReadOnly Property Key As String Get Return _Key End Get End Property Private _DataFieldName As String Public ReadOnly Property DataFieldName As String Get Return _DataFieldName End Get End Property Public Sub New(Key As String) _Key = Key _DataFieldName = Key End Sub Public Sub InstantiateIn(container As System.Web.UI.Control) Implements ITemplate.InstantiateIn Dim chk = New CheckBox() chk.ID = Key container.Controls.Add(chk) End Sub End Class
I think I got this code from one of the posts on this forum.
I would really appreciate any help converting this Declarative column definition to Code:
<ig:TemplateDataField Header-Text="" Key="chkSel" Width="30"> <ItemTemplate> <input type="checkbox" id="chkSel" runat="server" /> </ItemTemplate> </ig:TemplateDataField>
Regards,pp James Kydd
I’ve looked into your issue and it seems that the button’s click event is too late in the page lifecycle to re-create the template.
Instead you could check on Page_Load or earlier which control has caused the postback and if needed clear and recreate the columns there.
Otherwise your code for creating the template seems ok. Generally you can check out how to create an item template in code behind here:
http://help.infragistics.com/NetAdvantage/ASPNET/2012.2/CLR4.0/?page=WebDataGrid_Using_Item_Template.html
Let me know if you have any questions or if you need further assistance.