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

Custom ItemTemplate - lost after postback

1 Answer 109 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
PAYDAY
Top achievements
Rank 1
PAYDAY asked on 04 Aug 2009, 12:18 PM
I've created a custom implementation of the RadComboBox control and I'm having problems after postback. Seems the ItemTemplate is lost after the page posts back and the ItemText is what shows in the list instead of the ItemDescription I've specified. Here's my code:

//I do not want to reload data after postback so I check to see if items have already been loaded
    if (MyCustomComboBox.Items.Count < 1)
    {
            MyCustomComboBox.DataTextField = "ItemText";
            MyCustomComboBox.DataValueField = "ItemValue";
            MyCustomComboBox.DataListField = "ItemDescription";
            MyCustomComboBox.DataSource = CEarn.GetECodeList();
            MyCustomComboBox.DataBind();

            MyCustomComboBox.Items.Insert(0, new RadComboBoxItem("Select One...", String.Empty));
    }



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;

namespace CustomWebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomComboBox runat=server></{0}:CustomComboBox>")]
    public class CustomComboBox : RadComboBox
    {
        #region Members
        private string _dataListField = String.Empty;

        #endregion

        #region Properties
        /// <summary>
        /// Specifies field to use for dropdown list items
        /// </summary>
        public string DataListField
        {
            get
            {
                return _dataListField;
            }
            set
            {
                _dataListField = value;
                this.ItemTemplate = new ItemTemplate(_dataListField);
            }
        }

        #endregion

    }

    /// <summary>
    /// Creates ItemTemplate control inside combobox and binds with specified field data
    /// </summary>
    public class ItemTemplate : ITemplate
    {
        #region Members
        private string _dataListField = String.Empty;
        #endregion

        #region Properties
        /// <summary>
        /// Specifies field to use for dropdown list items
        /// </summary>
        public string DataListField
        {
            get
            {
                return _dataListField;
            }
            set
            {
                _dataListField = value;
            }
        }
        #endregion

        #region Methods
        public ItemTemplate() { }

        public ItemTemplate(string dataListField)
        {
            this.DataListField = dataListField;
        }

        public void InstantiateIn(Control container)
        {
            HtmlTable table = new HtmlTable();
            HtmlTableRow row = new HtmlTableRow();

            HtmlTableCell ComboBoxCell = new HtmlTableCell();
            ComboBoxCell.DataBinding += new EventHandler(ComboBoxCell_DataBinding);
            row.Controls.Add(ComboBoxCell);

            table.Controls.Add(row);
            container.Controls.Add(table);
        }
        #endregion

        #region Events
        private void ComboBoxCell_DataBinding(object sender, EventArgs e)
        {
            try
            {
                HtmlTableCell target = (HtmlTableCell)sender;
                RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;                
                string itemText = (string)DataBinder.Eval(item.DataItem, DataListField);
                target.InnerText = itemText;                
            }
            catch
            {
                throw new InvalidOperationException("When using CustomComboBox, you must specify a value for the DataListField property.");
            }
        }
        #endregion
    }

}

1 Answer, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 07 Aug 2009, 08:09 AM
Hi Richard,

Please, make sure that you're setting the ItemTemplate each time in the OnInit method:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    MyCustomComboBox.ItemTemplate = new ItemTemplate();
}

You have to do so, as the template can't be persisted in the ViewState. I hope this helps.

Regards,
Tsvetomir Tsonev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
ComboBox
Asked by
PAYDAY
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Share this question
or