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

ComboBox dynamic adding checkboxes to template

1 Answer 95 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Henrik Tegman
Top achievements
Rank 1
Henrik Tegman asked on 01 Mar 2011, 09:29 AM
Hello,

I've been searching the forums and found some similar topics but I still can't get it to work so I thought I'd post my own to see what I'm doing wrong.

My situation is simple, I have a ComboBox and I want the dropdown to have checkboxes, as I understand there is no easy way to just add a checkbox to a ComboBox, instead I have to use the ItemTemplate.

At the moment I have a class that uses the ITemplate, it has a InstantiateIn method and there it gets the "dynamic" data from a DB.

It looks something like this, don't mind the extra code it's still very early and just trying to get things to work:

public void InstantiateIn(Control container)
        {
            object[] searchoptions = GetProxy().GetSearchOptionList();
            DataSet so = (DataSet)searchoptions[0];
            List<SearchOption> searchOpt = new List<SearchOption>();
            for (int k = 0; k < so.Tables.Count; k++)
                for (int j = 0; j < so.Tables[k].Rows.Count; j++)
                {
                    SearchOption search = new SearchOption();
                    search.ID = so.Tables[k].Rows[j].ItemArray[0].ToString();
                    search.name = so.Tables[k].Rows[j].ItemArray[1].ToString();
                    searchOpt.Add(search);
                    CheckBox box = new CheckBox();
                    box.Text = search.name;
                    box.ID = search.ID;
                    box.DataBinding += new EventHandler(box_DataBinding);
                    container.Controls.Add(box);
                }
}

It simply loops over it and takes out all the data, and attaches a Eventhandler

private void box_DataBinding(object sender, EventArgs e)
        {
            CheckBox target = (CheckBox)sender;
            RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;
            string itemText = (string)DataBinder.Eval(item, "Text");
            target.Text = itemText;
        }

But this doesn't work, the biggest problem is that I don't really get it myself why I have to go on with Templates and all this, I just want a dropdown with checkboxes, so maybe I'm overcomplicating things here or maybe I just don't understand.

Any help is welcome.

1 Answer, 1 is accepted

Sort by
0
Helen
Telerik team
answered on 01 Mar 2011, 03:43 PM
Hi Henrik,

Did you review the following help topic:

http://www.telerik.com/help/aspnet-ajax/combo_templatesaddingtemplates.html?

Did you try to follow the code in the "Assigning ItemTemplate at runtime" section?

Regards,
Helen
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!
Tags
ComboBox
Asked by
Henrik Tegman
Top achievements
Rank 1
Answers by
Helen
Telerik team
Share this question
or