I need to create a combobox with variable number of columns. Both the number of columns, and the column names would be determined only at run time. Also, I need the combobox to be a multiselect combo, so I need to put in a checkbox in the item template. So, I am creating the item template and header template at runtime.
When I run the code, I am getting the error "Multiple controls with the same ID 'chkSourceElementValue' were found. FindControl requires that controls have unique IDs." where chkSourceElementValue is the ID of the checkbox thats created in the item template. I am setting the item template, header template, data source at page_load and also data binding it at that point. I have enclosed snippets of my code, as well as the error message stack below. it looks like the error is originating from within RadComboBox.
I am using the latest version of Telerik ASP.NET AJAX Controls (ASP.NET AJAX Q3 2010 NET 35)
Please advise at the earliest. Thank you.
Page_Load:
this
.rcbSourceElement.ItemTemplate = new SourceElementValueDropDownTemplate(ListItemType.Item, iCountOfColumnsInDataset, mColumnNamesToDisplay);
this.rcbSourceElement.HeaderTemplate = new SourceElementValueDropDownTemplate(ListItemType.Header, iCountOfColumnsInDataset, mColumnNamesToDisplay);
this.rcbSourceElement.DataSource = dsSourceElementValuesForDisplay;
this.rcbSourceElement.DataBind();
Template Class
public class SourceElementValueDropDownTemplate : ITemplate
{
PlaceHolder ph = new PlaceHolder();
private ListItemType CurrentTemplateType { get; set; }
private int CountOfColumns { get; set; }
private string[] ColumnHeaders { get; set; }
public SourceElementValueDropDownTemplate(ListItemType type, int countOfColumnsInDropDown, string[] columnHeaders)
{
CurrentTemplateType = type;
CountOfColumns = countOfColumnsInDropDown;
ColumnHeaders = columnHeaders;
}
void ITemplate.InstantiateIn(Control container)
{
CheckBox chkSourceElementValue;
Label lblSourceElementValue;
switch(CurrentTemplateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl("<ul>"));
foreach(string mColName in ColumnHeaders)
{
ph.Controls.Add(new LiteralControl("<li class='rcbColumn'>"));
ph.Controls.Add(new LiteralControl(mColName));
ph.Controls.Add(new LiteralControl("</li>"));
}
ph.Controls.Add(new LiteralControl("</ul>"));
container.Controls.Add(ph);
break;
case ListItemType.Item:
ph.Controls.Add(new LiteralControl("<ul>"));
for (int iRCbColCtr = 1; iRCbColCtr < CountOfColumns ; iRCbColCtr++) /* First column of the dataset is expected to be the ELEMENT_VALUE_KEY. No need to show that in a column */
{
ph.Controls.Add(new LiteralControl("<li class='rcbColumn'>"));
if (iRCbColCtr == 1) // This is the first column - must appear as a checkbox
{
chkSourceElementValue = new CheckBox();
chkSourceElementValue.ID = "chkSourceElementValue";
chkSourceElementValue.Attributes.Add("ColIndex", iRCbColCtr.ToString());
chkSourceElementValue.DataBinding += new EventHandler(chkSourceElementValue_DataBinding);
//chkSourceElementValue.CheckedChanged += new EventHandler(chkSourceElementValue_CheckedChanged);
ph.Controls.Add(chkSourceElementValue);
}
else
{
lblSourceElementValue = new Label();
lblSourceElementValue.ID = "lblSourceElementValue";
lblSourceElementValue.Attributes.Add("ColIndex", iRCbColCtr.ToString());
lblSourceElementValue.DataBinding += new EventHandler(lblSourceElementValue_DataBinding);
ph.Controls.Add(lblSourceElementValue);
}
ph.Controls.Add(new LiteralControl("</li>"));
}
ph.Controls.Add(new LiteralControl("</ul>"));
//ph.DataBinding += new EventHandler(ph_DataBinding);
container.Controls.Add(ph);
break;
}
}
void chkSourceElementValue_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkSourceElementValue = sender as CheckBox;
RadComboBox rcbParentCombo = chkSourceElementValue.Parent as RadComboBox;
}
void lblSourceElementValue_DataBinding(object sender, EventArgs e)
{
Label lblSourceElementValue = (sender) as Label;
int iColIndex = Convert.ToInt16(lblSourceElementValue.Attributes["ColIndex"]);
RadComboBoxItem container = (RadComboBoxItem)lblSourceElementValue.NamingContainer;
lblSourceElementValue.Text = ((DataRowView)(container.DataItem))[iColIndex].ToString();
}
void chkSourceElementValue_DataBinding(object sender, EventArgs e)
{
CheckBox chkSourceElementValue = (sender) as CheckBox;
int iColIndex = Convert.ToInt16(chkSourceElementValue.Attributes["ColIndex"]);
RadComboBoxItem container = (RadComboBoxItem)chkSourceElementValue.NamingContainer;
chkSourceElementValue.Text = ((DataRowView)(container.DataItem))[iColIndex].ToString();
}
}
Error Received :
[HttpException (0x80004005): Multiple controls with the same ID 'chkSourceElementValue' were found. FindControl requires that controls have unique IDs.]
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +273
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +320
System.Web.UI.Control.EnsureNamedControlsTable() +61
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +222
System.Web.UI.Control.FindControl(String id) +12
Telerik.Web.ChildControlHelper.FindControlRecursive(String ID, Control root) +182
Telerik.Web.ChildControlHelper.FindControlRecursive(String ID, Control root) +227
Telerik.Web.ChildControlHelper.FindControlRecursive(String ID, Control root) +227
Telerik.Web.ChildControlHelper.FindControlRecursive(String ID, Control root) +227
Telerik.Web.ChildControlHelper.FindControlRecursive(Control searcher, String ID) +186
Telerik.Web.UI.RadAjaxControl.OnPagePreRender(Object sender, EventArgs e) +1031
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnPreRender(EventArgs e) +8687734
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint