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

DataBinding event in ItemTemplate not triggered

1 Answer 128 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Michel Mathijssen
Top achievements
Rank 1
Michel Mathijssen asked on 20 Jul 2010, 02:32 PM
Hi All,

I'm creating a little custom control that encapsulates the RadCombobox.
For my custom control, I created an ItemTemplate that adds a checkbox and changes the background of the items added to the radcombobox.

It seems that the databinding events are not triggered...

In my ItemTemplate.CS i've added the following code:

public void InstantiateIn(Control container)
 {
     if (null != container)
     {
         // Create the panel
         using (Panel categoryPanel = new Panel())
         {
             categoryPanel.ID = "CategoriesDropDownPanel";
             categoryPanel.Width = Unit.Percentage(100);
             categoryPanel.DataBinding += new EventHandler(CategoryPanel_DataBinding);
 
             // Create the checkbox
             using (CheckBox checkBox = new CheckBox())
             {
                 checkBox.ID = "CategoriesDropDownCheckBox";
                 checkBox.Text = "Category";
                 checkBox.DataBinding += new EventHandler(CheckBox_DataBinding);
                 categoryPanel.Controls.Add(checkBox);
             }
             container.Controls.Add(categoryPanel);
         }
     }
 }

private void CategoryPanel_DataBinding(object sender, EventArgs e)
{
    // TODO: Never gets called!
    Panel target = (Panel)sender;
    RadComboBoxItem container = (RadComboBoxItem)target.BindingContainer;
    Category category = (Category)container.DataItem;
    target.BackColor = System.Drawing.Color.FromArgb(category.Color);
}

private void CheckBox_DataBinding(object sender, EventArgs e)
{
    // TODO: Never gets called!
    CheckBox target = (CheckBox)sender;
    RadComboBoxItem container = (RadComboBoxItem)target.BindingContainer;
    target.Text = (string)DataBinder.Eval(container.DataItem, "Name");
}

I've been able to get around this, by adding the code to the of RadCategoryDropDown_ItemDataBound my custom control.
My Custom control looks like:

using System;
using System.Globalization;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections.ObjectModel;
using System.Web.UI.HtmlControls;
using System.Data;
using CategoryDropDownTest.Entities;
 
namespace CategoryDropDownTest.CustomControls
{
    /// <summary>
    /// USG Web Server Control for displaying a category dropdown in a website
    /// </summary>
    [ToolboxData("<{0}:CategoryDropDown runat=server></{0}:CategoryDropDown>"),
     /*ParseChildren(false),*/
    ParseChildren(ChildrenAsProperties = true),
    PersistChildren(false),
    DefaultProperty("Text")]
    public class CategoryDropDown : CompositeControl, INamingContainer
    {
 
        #region Private Members
 
        private RadComboBox radCategoryDropDown;
        private Collection<Category> items = new Collection<Category>();
 
        #endregion
 
        #region Public Properties
 
        /// <summary>
        /// gets the collection of Category
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        MergableProperty(false),
        Category("CategoryDropDown Properties"), Description("Categories of the dropdown menu.")]
        public Collection<Category> Items
        {
            get { return items; }
        }
         
        /// <summary>
        /// Gets or sets the Text to be displayed
        /// </summary>
        [Category("CategoryDropDown Properties"), Description("Text for the categoryDropDown")]
        public string Text { get; set; }
 
        #endregion
 
        #region Overriden Methods
 
        #region RenderContents
 
        /// <summary>
        /// Overridden RenderContents
        /// </summary>
        /// <param name="writer">HmltTextWriter parameter</param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
 
            if (this.DesignMode)
            {
                // In design mode, display only the id
                if (writer != null) writer.WriteLine(this.ID);
            }
            else
            {
            }
            base.RenderContents(writer);
        }
 
        #endregion
 
        #region OnInit
 
        /// <summary>
        /// Overridden OnInit
        /// </summary>
        /// <param name="e">EventArfs parameter</param>
        protected override void OnInit(EventArgs e)
        {
            // Initialize the controls
            this.InitControl();
            base.OnInit(e);
        }
 
        #endregion
 
        #region OnLoad
 
        /// <summary>
        /// Overriden OnLoad
        /// </summary>
        /// <param name="e">EventArgs parameter</param>
        protected override void OnLoad(EventArgs e)
        {
            // Load the control
            this.LoadControl();
            base.OnLoad(e);
        }
 
        #endregion
 
        #endregion
 
        #region Private Methods
 
        /// <summary>
        /// Initialize the control
        /// </summary>
        private void InitControl()
        {
            // Create the RadCategoryDropDown
            this.radCategoryDropDown = new RadComboBox();
        }
 
        #region LoadControl
 
        /// <summary>
        /// Load the control
        /// </summary>
        private void LoadControl()
        {
            // Set fixed properties
            //this.radCategoryDropDown.HeaderTemplate = new HeaderTemplate();
            this.radCategoryDropDown.ItemTemplate = new ItemTemplate();
            this.radCategoryDropDown.ChangeTextOnKeyBoardNavigation = false;
            this.radCategoryDropDown.EnableTextSelection = false;
            this.radCategoryDropDown.HighlightTemplatedItems = true;
            this.radCategoryDropDown.EnableViewState = false;
 
            // Bind the events
            this.radCategoryDropDown.ItemDataBound += new RadComboBoxItemEventHandler(RadCategoryDropDown_ItemDataBound);
          
            // Add the properties
            this.radCategoryDropDown.Text = this.Text;
            if ((null != this.items) && (this.items.Count > 0))
            {
                this.radCategoryDropDown.DataSource = this.items;
                this.radCategoryDropDown.DataTextField = "Name";
                this.radCategoryDropDown.DataValueField = "Id";
                this.radCategoryDropDown.DataBind();
            }
 
            // Add the control
            this.Controls.Add(radCategoryDropDown);
        }
 
        #endregion
 
        #region Events
 
        /// <summary>
        /// ItemDataBound event of the radCategoryDropDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RadCategoryDropDown_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            if (e != null)
            {
                // Get the category
                Category category = (Category) e.Item.DataItem;
                // Find the panel
                Panel itemPanel = (Panel)e.Item.FindControl("CategoriesDropDownPanel");
                if (null != itemPanel)
                {
                    // TODO: Check if it can be added through the item template
                    //itemPanel.BackColor = System.Drawing.Color.FromArgb(category.Color);
                    // Find the checkbox
                    CheckBox itemCheckBox = (CheckBox) itemPanel.FindControl("CategoriesDropDownCheckBox");
                    if (null != itemPanel)
                    {
                        // TODO: Remove, should be added through the item template
                        //itemCheckBox.Text = category.Name;
                    }
                }
            }
        }
 
        #endregion
 
        #endregion
 
    }
}

However, I believe this is not the right way to do this.

Anyone an idea how to get the databinding events in the ItemTemplate to work? Perhaps It's possible to set the values in the InstantiateIn Mehtod???

Kind regards,

Michel

1 Answer, 1 is accepted

Sort by
0
Helen
Telerik team
answered on 23 Jul 2010, 09:36 AM
Hello Michel,

We are not exactly sure what might be causing the problem. Please find attached a project which is working at our side. Does it work at your side?

Could you try to modify it so we can see the problem?

Sincerely yours,
Helen
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
ComboBox
Asked by
Michel Mathijssen
Top achievements
Rank 1
Answers by
Helen
Telerik team
Share this question
or