This is a migrated thread and some comments may be shown as answers.

GridTemplateColumn inside a CompositeControl

4 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 2
Igor asked on 08 Jan 2014, 08:45 AM

Hello, I am trying to follow this sample

http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

to create a template grid column in a grid that in my case is contained inside a CompositeControl


First attempt, using the control OnInit event

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
 
 
if (!Page.IsPostBack)
{
    var boundColumn = new GridBoundColumn();
  grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = "MyColumn";
GridTemplateColumn templateColumn = new GridTemplateColumn();
grid.MasterTableView.Columns.Add(templateColumn);
templateColumn.ItemTemplate = new    MyTemplate(templateColumnName);
}
}

Result: the InstantiateIn method of the template is called only the first time, but in postbacks not.

Second attempt: subscribing to the page Init event

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
Page.Init += PageInit;
 
 
private void PageInit(object sender, EventArgs e)
{

var boundColumn = new GridBoundColumn();
  boundColumn.DataField = "MyColumn";

grid.MasterTableView.Columns.Add(boundColumn);
 
  GridTemplateColumn templateColumn =
new GridTemplateColumn();
templateColumn.ItemTemplate = new    MyTemplate(templateColumnName);
grid.MasterTableView.Columns.Add(templateColumn);

Result: the template column is correctly created, but the bound one is duplicated on every postback

What's the correct way to programmatically create a template column in a composite control?
Thanks

4 Answers, 1 is accepted

Sort by
0
Igor
Top achievements
Rank 2
answered on 08 Jan 2014, 11:16 AM

Ok, after one night sleeping I solved this issue.

The right solution is the first one, that is creating the GridTemplateColumn inside the OnInit control event.

The only problem is that during postbacks all info are retrieved from the ViewState except the ItemTemplate property, so you have to provide it on every postback.

But during OnInit the columns are not already rebuilt from the viewstate, so you have to wait a little bit later, for example during OnLoad



protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    ((GridTemplateColumn)grid.MasterTableView.Columns[1]).ItemTemplate = new MyTemplate(templateColumnName)
 
}


0
Igor
Top achievements
Rank 2
answered on 08 Jan 2014, 11:53 AM

Here's a second issue... but we're really close to the solution.

Now this grid has child rows, the GridTemplateColumn is in the master grid view and the child rows are simple GridBoundColumns.

When I expand one master row to see children, the GridTemplateColumn is still in the right place, but the InstantiateIn methods are not called so the column is empty.

0
Angel Petrov
Telerik team
answered on 13 Jan 2014, 07:55 AM
Hello Igor,

It would be difficult to provide a precise answer regarding this matter without having a better understanding of the exact implementation. Could you please provide us with the markup and code-behind of the page so we could investigate further?

Additionally note that when creating templates programmatically this should be done in the PageInit event. If the OnLoad event is used probably the template is not initialized correctly.

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Igor
Top achievements
Rank 2
answered on 13 Jan 2014, 08:24 AM
Thanks, I've choosen another path.
Tags
Grid
Asked by
Igor
Top achievements
Rank 2
Answers by
Igor
Top achievements
Rank 2
Angel Petrov
Telerik team
Share this question
or