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

[Solved] Trying to dynamically create a RadComboBox

3 Answers 338 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Lina
Top achievements
Rank 1
Lina asked on 12 Mar 2008, 07:05 PM
Hi,

I'm trying to create a combobox dynamically and populate it with data from an XML file. 

I am able to get the combobox to show on the page with the first item populated but nothing happens when I click on the arrow to display the rest of the items.  (In debug mode I can see that the OnDisplayDatabinding method is being called for every item and that the label is set with the correct data.)

Please take a look at my code and let me know what I'm doing wrong!

Thanks,
Lina

public partial class DynamicCombo : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        XmlDataSource1.XPath = "/filters/whiteboard/product[@id='WEATHER']/filters/region/option";  
 
        //create a new combobox  
        RadComboBox comboBoxRegion = new RadComboBox();  
        this.Controls.Add(comboBoxRegion);  
 
        comboBoxRegion.ID = "comboBoxRegion";  
        comboBoxRegion.DataTextField = "value";  
        comboBoxRegion.DataSourceID = XmlDataSource1.ID;  
        comboBoxRegion.ItemTemplate = new cbItemTemplate();  
        comboBoxRegion.DataBind();  
 
    }  
}  
 
public class cbItemTemplate : ITemplate  
{  
    public void InstantiateIn(Control container)  
    {  
        Label label = new Label();  
        label.ID = "ItemLabel";  
        label.Text = "Text";  
        label.DataBinding += new EventHandler(OnDisplayDataBinding);  
        container.Controls.Add(label);  
    }  
 
    void OnDisplayDataBinding(object sender, EventArgs e)  
    {  
        Label label = (Label)sender;  
        RadComboBoxItem comboboxitem = (RadComboBoxItem)label.BindingContainer;  
 
        label.Text = (string)DataBinder.Eval(comboboxitem, "Text");  
    }  

3 Answers, 1 is accepted

Sort by
0
Lina
Top achievements
Rank 1
answered on 13 Mar 2008, 09:42 PM
Hi,

Resolved the issue by adding the combobox to a Placeholder instead of adding directly to the Page.

But now I'm seeing another issue-  I put the code to generate the combobox into a helper method to which I pass the XPath.  If I call the method more than once to generate more than one combobox, all the comboboxes have the exact same data.  The XMLDataSource is the same but I'm setting a different XPath and calling databind() each time but when the page reloads all the comboboxes have the data as per the last passed in XPath.

I tried creating a new XMLDataSource dynamically each time I create a new Combobox but that didn't work.

Is there any way around this?


Thanks,
Lina
0
Accepted
fightclub777
Top achievements
Rank 1
answered on 14 Mar 2008, 03:46 AM
I had a similar issue in regards to both your problems.   Make sure that you create a new instance of the ComboBox for each item in your loop.  Don't create the instance of the ComboBox outside of the loop.

As for not being able to click on the drop down, make sure that each instance of the ComboBox has a unique ID.   I was dynamically creating the controls, and missed the fact that they were being created with the same ID.  Usually, .NET throws an exception for this scenario, but I have been able to do this over and over again.

Telerik, you might want to look into the ID issue. (I have a test project, if you wish to recreate the problem).  It caused me a few hours of frustration before realizing the problem.  Everything renders fine and shows on the page, but it would not let you click on the drop down.

Also, having a Prometheus comobox inside of a classic radajaxpanel produced the same result of not being able to select the drop down.
0
Lina
Top achievements
Rank 1
answered on 14 Mar 2008, 01:24 PM
Thanks fightclub777

I had not set an ID for the dynamically created XMLDataSource but was using it when assigning it as the DataSourceID for the RadComboBox.  So now along with the RadComboBox I create a new instance XMLDataSource each time.


Tags
ComboBox
Asked by
Lina
Top achievements
Rank 1
Answers by
Lina
Top achievements
Rank 1
fightclub777
Top achievements
Rank 1
Share this question
or