I followed the example found in your documentation at the link below on programmatically creating template columns.
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html#Creating_template_columns_programmatically
This works great for the initial data binding of the grid. However, on a subsequent postback when I call Rebind the output for the column with custom item template is blank. When I use the debugger to step through the NeedDataSource event handler it appears the custom item template is no longer associated with the column. The ItemTemplate property on the column is now null.
I followed the recommendation to add the new column to the collection before setting properties.
Other non-template columns on my grid which I created dynamically seem to retain their settings.
Any ideas on why the column item template was lost?
Thanks
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html#Creating_template_columns_programmatically
This works great for the initial data binding of the grid. However, on a subsequent postback when I call Rebind the output for the column with custom item template is blank. When I use the debugger to step through the NeedDataSource event handler it appears the custom item template is no longer associated with the column. The ItemTemplate property on the column is now null.
I followed the recommendation to add the new column to the collection before setting properties.
templateColumn = new GridTemplateColumn(); rgPlans.Columns.Add(templateColumn); templateColumn.HeaderText = "Custom Column"; templateColumn.ItemTemplate = new CustomFieldTemplate("MyCustomId"); Other non-template columns on my grid which I created dynamically seem to retain their settings.
Any ideas on why the column item template was lost?
Thanks
4 Answers, 1 is accepted
0
Hi John,
Indeed this is not expected behavior for the grid. The code snippet looks ok. Can you post a full code sample or runnable project so we can provide better assistance? I presume there might be some problems with binding of the column and the persistence in the ViewState. Please refer to the attached project based on the above mentioned help article and see what differs in your case.
Regards,
Marin
the Telerik team
Indeed this is not expected behavior for the grid. The code snippet looks ok. Can you post a full code sample or runnable project so we can provide better assistance? I presume there might be some problems with binding of the column and the persistence in the ViewState. Please refer to the attached project based on the above mentioned help article and see what differs in your case.
Regards,
Marin
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
John
Top achievements
Rank 1
answered on 07 Dec 2010, 07:14 PM
Hi Marin,
I've included sample code which demonstrates the problem.
Regards
John
I've included sample code which demonstrates the problem.
<telerik:RadGrid ID="MyGrid" runat="server" OnNeedDataSource="MyGrid_NeedDataSource" AutoGenerateColumns="false" /> <br /><br /> <asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click" Text="Rebind" /> protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { GridBoundColumn boundColumn = new GridBoundColumn(); MyGrid.Columns.Add(boundColumn); boundColumn.HeaderText = "My Key"; boundColumn.DataField = "ItemKey"; GridTemplateColumn templateColumn = new GridTemplateColumn(); MyGrid.Columns.Add(templateColumn); templateColumn.HeaderText = "Custom 1"; templateColumn.ItemTemplate = new CustomFieldTemplate("CustomField1"); templateColumn = new GridTemplateColumn(); MyGrid.Columns.Add(templateColumn); templateColumn.HeaderText = "Custom 2"; templateColumn.ItemTemplate = new CustomFieldTemplate("CustomField2"); } } protected void MyGrid_NeedDataSource(object sender, EventArgs e) { List<Widget> widgetList = new List<Widget>(); Widget widget = new Widget(); widgetList.Add(widget); widget.ItemKey = "1"; widget.CustomDataItemList = new List<CustomDataItem>(); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField1", CustomItemValue = "Blue" }); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField2", CustomItemValue = "Large" }); widget = new Widget(); widgetList.Add(widget); widget.ItemKey = "2"; widget.CustomDataItemList = new List<CustomDataItem>(); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField1", CustomItemValue = "Red" }); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField2", CustomItemValue = "Small" }); widget = new Widget(); widgetList.Add(widget); widget.ItemKey = "3"; widget.CustomDataItemList = new List<CustomDataItem>(); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField1", CustomItemValue = "Green" }); widget.CustomDataItemList.Add(new CustomDataItem { CustomItemKey = "CustomField2", CustomItemValue = "Medium" }); MyGrid.DataSource = widgetList; } protected void MyButton_Click(object sender, EventArgs e) { MyGrid.Rebind(); } private class Widget { public string ItemKey { get; set; } public List<CustomDataItem> CustomDataItemList { get; set; } } private class CustomDataItem { public string CustomItemKey {get;set;} public string CustomItemValue {get;set;} } private class CustomFieldTemplate : ITemplate { protected LiteralControl lControl; private string _customItemKey; public CustomFieldTemplate(string customItemKey) { _customItemKey = customItemKey; } public void InstantiateIn(System.Web.UI.Control container) { lControl = new LiteralControl(); lControl.DataBinding += new EventHandler(lControl_DataBinding); container.Controls.Add(lControl); } public void lControl_DataBinding(object sender, EventArgs e) { LiteralControl l = (LiteralControl)sender; GridDataItem container = (GridDataItem)l.NamingContainer; Widget widget = container.DataItem as Widget; CustomDataItem item = (from cdi in widget.CustomDataItemList where cdi.CustomItemKey == _customItemKey select cdi).FirstOrDefault(); l.Text = (item == null ? "{empty}" : item.CustomItemValue); } } Regards
John
0
Hello John,
When you add template column you have to do it in the Page_Init event so that they are persisted in the ViewState. Also the whole grid has to be created in the code behind. You need to follow the presented approach in the section Creating template columns programmatically in the help article.
Hope this helps.
Regards,
Marin
the Telerik team
When you add template column you have to do it in the Page_Init event so that they are persisted in the ViewState. Also the whole grid has to be created in the code behind. You need to follow the presented approach in the section Creating template columns programmatically in the help article.
Hope this helps.
Regards,
Marin
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
John
Top achievements
Rank 1
answered on 08 Dec 2010, 03:06 PM
Hi Marin,
I now see the warning footnote in the documentation about creating template columns inside the Page_Load event handler. I must have overlooked it because it was shown after the VB code sample which was of no interest to me. I will rework my code to create the grid inside the Page_Init event hander.
Thanks for your help!
Regards,
John
I now see the warning footnote in the documentation about creating template columns inside the Page_Load event handler. I must have overlooked it because it was shown after the VB code sample which was of no interest to me. I will rework my code to create the grid inside the Page_Init event hander.
Thanks for your help!
Regards,
John