Hi All,
In my ItemTemplate.CS i've added the following code:
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:
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
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