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
}
}
//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
}
}