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

Dynamically adding edit templates

2 Answers 146 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 17 Oct 2011, 11:15 PM
I've been able to add templates in code-behind using my own iTemplate class. I'm having problems getting the editItemTemplate to work.

I've tried a dozen variations and approaches - I can't find a single working example of adding both items, and editItemTemplates anywhere on the intarwebs.

Issues:
1) The edit text boxes don't appear after clicking the edit button, the row goes blank
2) I'm using Page_Init, only if not postback to add the columns to the grid
3) After paging, data disappears from the grid.
4) After clicking edit, ALL of the data in the template columns go away.

//add item templates
 public class RadGridEditTemplate : ITemplate
    {
        protected TextBox txtBox;
        private string colname;

        public RadGridEditTemplate(string cName)
        {
            colname = cName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox t = new TextBox();
            t.ID = colname;
            t.Width=Unit.Percentage(100);
            container.Controls.Add(t);
        }
    }

//add edit template
 public class RadGridEditTemplate : ITemplate
    {
        protected TextBox txtBox;
        private string colname;

        public RadGridEditTemplate(string cName)
        {
            colname = cName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            TextBox t = new TextBox();
            t.ID = colname;
            t.Width=Unit.Percentage(100);
            container.Controls.Add(t);
        }
    }

//code that attaches column templates to the grid
GridTemplateColumn col = new GridTemplateColumn();
            rg.MasterTableView.Columns.Add(col);

            col.HeaderText = header;
            col.AllowFiltering = true;
            col.DataField = dataField;
            col.UniqueName = dataField;
            col.Display = visible;
            col.ItemTemplate = new RadGridTemplate(dataField);
            col.EditItemTemplate = new RadGridEditTemplate(dataField);







2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Oct 2011, 11:15 AM
Hello Daniel,

I have tried recreating the issue at my end but no avail. Please take a look into the code which I tried.
C#:
public partial class RadGrid_ProgramaticTemp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
    {
 
    }
override protected void OnInit(EventArgs e) 
    
        DefineGridStructure(); 
        base.OnInit(e); 
    
private void DefineGridStructure() 
    {
        RadGrid1.AutoGenerateEditColumn = true;
        GridBoundColumn boundColumn; 
  
        //Important: first Add column to the collection 
        boundColumn = new GridBoundColumn(); 
        this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
        this.RadGrid1.AllowPaging = true;
        RadGrid1.PageSize = 4;
  
        //Then set properties 
        boundColumn.AllowSorting = true
        boundColumn.DataField = "EmployeeID"
        boundColumn.HeaderText = "EmployeeID"
        string templateColumnName = "ContactName"
  
        GridTemplateColumn templateColumn = new GridTemplateColumn(); 
        templateColumn.ItemTemplate = new MyItemTemplate(templateColumnName);
        templateColumn.EditItemTemplate = new MyEditTemplate(templateColumnName); 
        this.RadGrid1.MasterTableView.Columns.Add(templateColumn); 
        templateColumn.SortExpression = "EmployeeID"
        templateColumn.HeaderText = templateColumnName; 
        //templateColumn.DataField = "EmployeeID"; 
  
        GridBoundColumn boundColumn1 = new GridBoundColumn(); 
        this.RadGrid1.MasterTableView.Columns.Add(boundColumn1); 
        boundColumn1.AllowSorting = true
        boundColumn1.DataField = "LastName"
        boundColumn1.UniqueName = "LastName"
        boundColumn1.HeaderText = "LastName Column";         
    }
public class MyEditTemplate : IBindableTemplate
    {
 
        protected TextBox textBox;
        private string colname;
        public MyEditTemplate(string cName)
        {
            colname = cName;
        }
public void InstantiateIn(System.Web.UI.Control container)
        {
 
            textBox = new TextBox();
            textBox.ID = "templateColumnTextBox";
            textBox.Text = "test";
            container.Controls.Add(textBox);
        }
public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
        {
            OrderedDictionary od = new OrderedDictionary();
            od.Add("ProviderName", ((TextBox)(((GridEditFormItem)(container)).FindControl("templateColumnTextBox"))).Text);
            return od;
        }
    }
 private class MyItemTemplate : ITemplate 
    {      
        protected TextBox textBox;    
        private string colname;
        public MyItemTemplate(string cName) 
        
            colname = cName; 
        
public void InstantiateIn(System.Web.UI.Control container) 
        {            textBox = new TextBox(); 
            textBox.ID = "templateColumnTextBox"
                                   
            container.Controls.Add(textBox);            
        
    }
}

Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 18 Oct 2011, 04:17 PM
Thanks - that's very close to what I'm trying.

I took your approach and moved code into as PreInit override, and now I get the following error on any kind of postback (edit, paging, etc):

"Multiple controls with the same ID 'FilterTextBox_ID' were found. FindControl requires that controls have unique IDs. "

It's as if the columns are getting re-added to the form. Do you have viewstate off by chance?


Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Daniel
Top achievements
Rank 1
Share this question
or