GridTemplateColumn inside a CompositeControl

5 posts, 0 answers
  1. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 08 Jan 2014 Link to this post

    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
  2. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 08 Jan 2014 Link to this post

    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)
     
    }


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 08 Jan 2014 Link to this post

    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.

  5. Angel Petrov
    Admin
    Angel Petrov avatar
    1006 posts

    Posted 13 Jan 2014 Link to this post

    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.
  6. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 13 Jan 2014 Link to this post

    Thanks, I've choosen another path.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017