ComboBox dynamic adding checkboxes to template

2 posts, 0 answers
  1. Henrik Tegman
    Henrik Tegman avatar
    29 posts
    Member since:
    Apr 2010

    Posted 01 Mar 2011 Link to this post

    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.
  2. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 01 Mar 2011 Link to this post

    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!
Back to Top