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

Insert not working when using custom/template column

1 Answer 117 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Claus
Top achievements
Rank 1
Claus asked on 19 Nov 2012, 03:50 PM
Hello

I am developing a usercontrol using Radgrid. My problem is that when I insert a template column containing a RadAutoCompleteBox the insert command stops firing and the callback from the RadAutoCompleteBox inside the TemplateColumn is invalid. In Edit Mode everything works fine. If I remove the column from the grid the insert command starts firing again and works perfectly, any ideas as to what the problem might be? 

the code for my custom column is as follows:

public class GridLookupColumn : GridTemplateColumn
    {
        private string _lookupDataMember;
 
        public string LookupDataMember
        {
            get { return _lookupDataMember; }
            set { _lookupDataMember = value; }
        }
        private string _lookupTextField;
 
        public string LookupTextField
        {
            get { return _lookupTextField; }
            set { _lookupTextField = value; }
        }
        private string _lookupValueField;
 
        public string LookupValueField
        {
            get { return _lookupValueField; }
            set { _lookupValueField = value; }
        }
        public class LookupActionTemplate : IBindableTemplate
        {
            public GridLookupColumn Column = null;
            
            
            public void InstantiateIn(Control container)
            {
                RadAutoCompleteBox lookup = new RadAutoCompleteBox();
                lookup.ID ="LC" + this.Column.UniqueName;
                lookup.DataSource = Column.Owner.OwnerGrid.DataSource;
                lookup.DataMember = Column.LookupDataMember;
                lookup.DataTextField = Column.LookupTextField;
                lookup.DataValueField = Column.LookupValueField;
                lookup.EnableViewState = true;
                container.Controls.Add(lookup);
                 
                 
                lookup.DataBind();
                lookup.DataBinding += new EventHandler(LookupDataBindingHandler);
                lookup.EntryAdded += new AutoCompleteEntryEventHandler(EntryAddedHandler);
                 
            }
            private void EntryAddedHandler(object sender, EventArgs e)
            {
                RadAutoCompleteBox lookup = sender as RadAutoCompleteBox;
                if (lookup.Entries.Count > 1)
                {
                    lookup.Entries.RemoveAt(0);
                    ExtractValues(lookup.NamingContainer as GridDataItem);
                     
                }
            }
            private void LookupDataBindingHandler(object sender, EventArgs e)
            {
                RadAutoCompleteBox lookup = sender as RadAutoCompleteBox;
                GridDataItem item = (GridDataItem)lookup.NamingContainer;
                string id = DataBinder.Eval(item.DataItem, this.Column.DataField).ToString();
                string text = String.Empty;
                DataSet data = (DataSet)Column.Owner.DataSource;
                foreach (DataRow r in data.Tables[Column.LookupDataMember].Rows)
                {
                    if (r[Column.LookupValueField].ToString() == id)
                    {
                        text = r[Column.LookupTextField].ToString();
                    }
                }
                AutoCompleteBoxEntry entry = new AutoCompleteBoxEntry(text, id);
                if(text != String.Empty)
                lookup.Entries.Add(entry);
                 
                             
                 
            }
 
            public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
            {
                IOrderedDictionary values = new OrderedDictionary();
                RadAutoCompleteBox lookup = container.FindControl("LC" + Column.UniqueName) as RadAutoCompleteBox;
                values.Add(Column.UniqueName, lookup.Entries[0].Value);
                return values;
            }
        }
        public class LookupTemplate : ITemplate
        {
            public GridLookupColumn Column = null;
            public void InstantiateIn(Control container)
            {
                Label l = new Label();
                l.ID = Column.UniqueName;
                container.Controls.Add(l);
                l.DataBinding += new EventHandler(LookupDataBindingHandler);
            }
            private void LookupDataBindingHandler(object sender, EventArgs e)
            {
                Label l = sender as Label;
                GridDataItem item = (GridDataItem)l.NamingContainer;
                string id = DataBinder.Eval(item.DataItem, this.Column.DataField).ToString();
                DataSet data = (DataSet) Column.Owner.DataSource;
                foreach (DataRow r in data.Tables[Column.LookupDataMember].Rows)
                {
                    if (r[Column.LookupValueField].ToString() == id)
                    {
                        l.Text = r[Column.LookupTextField].ToString();
                    }
                }
                  
 
            }
        }
         
        public GridLookupColumn()
        {
             
            LookupActionTemplate lookupActionTemplate = new LookupActionTemplate();
            LookupTemplate lookupTemplate = new LookupTemplate();
            lookupTemplate.Column = this;
            lookupActionTemplate.Column = this;
            this.EditItemTemplate = lookupActionTemplate;
            this.ItemTemplate = lookupTemplate;
            this.InsertItemTemplate = lookupActionTemplate;
            
             
             
             
        }
    }

I create the grid on Page Load as described in the documentation.

Any help is highly appreciated

Best Regards,

Claus Veirup

1 Answer, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 22 Nov 2012, 08:13 AM
Hello Claus,

The experienced behavior is caused from the fact you are creating a template column in the Page_Load event. As stated in the help article below - "Creating columns in the Page_Load event handler does not work for template columns. For the controls inside a template to persist their ViewState, the grid must be generated completely in the code-behind using the Page_Init event (see below). That way, template controls are instantiated before the LoadViewState event of the page."

In order to resolve your issue you should move the creation of the RadGrid from Page_Load to Page_Init event.

All the best,
Antonio Stoilkov
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Claus
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Share this question
or