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

Programmatic creation, GridTemplateColumn and ItemTemplate

3 Answers 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexandre BARBIER
Top achievements
Rank 1
Alexandre BARBIER asked on 25 Aug 2010, 11:03 AM
I need to create a grid entirely by code, assigning it some custom columns (GridTemplateColumn) and theses columns have HeaderTemplate, ItemTemplate and EditItemTerplate set with a custom class wich implements ITemplate.
I just followed this article : http://www.telerik.com/help/aspnet/grid/grdprogrammaticcreation.html
Each of my columns use an new instance of the same 'Itemplate class'

here is the content of InstanciateIn method into ItemTemplate class :
public void InstantiateIn(Control container)
        {
            ctrlAcquis = new Literal();
            ctrlAcquis.DataBinding += new EventHandler(ctrl1_DataBinding);
            Table tb = new Table(); 
            TableRow r = new TableRow();
            TableCell c1 = new TableCell();
            TableCell c2 = new TableCell();
            r.Cells.Add(c1);
            r.Cells.Add(c2);
            tb.Rows.Add(r);
  
            c1.Controls.Add(ctrlAcquis);
                          
            container.Controls.Add(tb);
        }

It is working fine !

But now, to be able to export this custom cxolumns in CSV format, I see here : http://www.telerik.com/help/aspnet-ajax/grid-csv-export.html that I need to Iterate throught each of my items , use FindControl to find my Literal, get content and assign it to my item Text property. Something like that :
foreach(GridDataItem item in RadGrid1.MasterTableView.Items)
   {
       Image img = item["MyColumn"].FindControl("Image1") as Image;
       item["MyColumn"].Text = img.AlternateText;
   }

OK, so I need to assign a ID to my LiteralctrlAcquis
Here is the new code of my item template :
public void InstantiateIn(Control container) 
        
            ctrlAcquis = new Literal(); 
            ctrlAcquis.ID = "ctrlAcquis";
            ctrlAcquis.DataBinding += new EventHandler(ctrl1_DataBinding); 
            Table tb = new Table();  
            TableRow r = new TableRow(); 
            TableCell c1 = new TableCell(); 
            TableCell c2 = new TableCell(); 
            r.Cells.Add(c1); 
            r.Cells.Add(c2); 
            tb.Rows.Add(r); 
    
            c1.Controls.Add(ctrlAcquis); 
                            
            container.Controls.Add(tb); 
        }

My grid show up ok at first display, but at the first postback I got this error :
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'ctrlAcquis' were found. FindControl requires that controls have unique

How to set up unique id on all instance of my item template, but stay able to use FindControl("ctrlAcquis") ?
Do I need to name my control with the name of the custom colum for instance because each of my columns use the same Itemplate class ?

p.s: I'm using version 2010.1.519.35 of Telerik RadControls for ASP.NET Ajax

Thanks for help !

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 25 Aug 2010, 11:23 AM
Hi Alexandre,

You may consider passing column's UniqueName to the template instance and use its value to build an unique id value for the controls inside the template.

All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mike
Top achievements
Rank 1
answered on 01 Sep 2010, 08:18 PM
I'm having a similar problem.  I am adding Label controls to my grid cells dynamically in the OnItemCreated handler...

 

protected void OnItemCreated(object sender, GridItemEventArgs args)

 

{

 

    if (args.Item is GridDataItem)

 

    {

 

        GridDataItem griditem = (GridDataItem) args.Item;

 

 

        foreach (GridColumn column in FlowSheetGrid.MasterTableView.RenderColumns)

 

        {

 

            Label lbl = new Label();

 

            lbl.ID =

"ValueLbl";

 

            griditem[column].Controls.Add(lbl);

        }

    }

}

But when I try to use FindControl to find the Label control in my OnItemDataBound handler, I get a "Multiple controls with the same ID 'Text' were found."  OnItemDataBound handler code...

if (args.Item is GridDataItem)

 

{

 

    GridDataItem griditem = (GridDataItem) args.Item;

 

 

    foreach (GridColumn column in FlowSheetGrid.MasterTableView.RenderColumns)

 

    {

 

        Label lbl = (Label)griditem[column].FindControl("ValueLbl");  // Error thrown here

 

    

}

 

 

}

Stepping through with the debugger and looking at the Controls array I can see that all the Label controls have the same ClientID and same UniqueID which is indeed "..._Text" and "...$Text" respectively.  These should be getting unique ids.  Why aren't they.  Is this an execution order issue?

Not sure if it matters but the columns in question are created dynamically as GridTemplateColumns...

 

 

GridTemplateColumn col = new GridTemplateColumn();

 

col.ItemTemplate =

new FlowSheetTemplate();

 

col.DataField = dataColumn.ColumnName;

col.HeaderText = dataColumn.ColumnName;

col.UniqueName = dataColumn.ColumnName;  // This has to be set otherwise the uniquenames are "TemplateColumn1", etc.

FlowSheetGrid.MasterTableView.Columns.Add(col);


0
Rosen
Telerik team
answered on 02 Sep 2010, 06:45 AM
Hi Mike,

The IDs will be unique only if you are adding the control in a different INamingContainers such as RadGrid's Items. However, cells are not INamingContainers, therefore you should ensure ID's uniqueness yourself when adding controls to cell's control collection.

Regards,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Alexandre BARBIER
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Mike
Top achievements
Rank 1
Share this question
or