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

Dynamically creating multi column dropdown, receiving error "Multiple controls with the same ID were found"

4 Answers 112 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ashwin Kumar
Top achievements
Rank 1
Ashwin Kumar asked on 20 Feb 2011, 09:56 PM

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
 

4 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 23 Feb 2011, 03:52 PM
Hi Ashwin,

The error indicates that all the checkboxes have the same ID, please change it like this:

chkSourceElementValue.ID = "chkSourceElementValue" + iRCbColCtr.ToString()

also the same change is needed for lblSourceElementValue element.

Hope this helps.

All the best,
Yana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ashwin Kumar
Top achievements
Rank 1
answered on 23 Feb 2011, 10:08 PM
No, that doesnt do it either.  The variable iRcbColCtr is used to track the column number.  I had already tried using that as a suffix for the ID, but when I do, I just get the error as "Multiple controls with the same ID 'chkSourceElementValue1' were found. FindControl requires that controls have unique IDs.]".  When I debugged the code, I found that the first line is generated fine, this problem comes when it tries to create the second row.

I cant create a unique id for every row, because when I need to use "FindControl" during databinding, I cant determine which ROW fired the event.  I have generated other custom controls this way, and I have never had to generate unique Ids for each row in an ItemTemplate.

Please advise.

0
Yana
Telerik team
answered on 01 Mar 2011, 10:44 AM
Hello Ashwin,

You're complete right about the id of the controls inside template - I'm sorry for my mistake.

I've tested your code but wasn't able to reproduce this error. Could you please send us a simple runnable page demonstrating the problem? You should open a support ticket and attach it there. Thanks

All the best,
Yana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ashwin Kumar
Top achievements
Rank 1
answered on 08 Mar 2011, 05:40 PM
I will have to get back to you on this... for now, I have hardcoded the columns - not something I would want to do, but for lack of time, I had to.

But I will probably get back when I get back on making the columns dynamic again.        
Tags
ComboBox
Asked by
Ashwin Kumar
Top achievements
Rank 1
Answers by
Yana
Telerik team
Ashwin Kumar
Top achievements
Rank 1
Share this question
or