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

[Solved] Creating templated RAD Combo Box at runtime

3 Answers 181 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 08 Jan 2010, 07:54 PM
I need to dynamically create RAD Combo Boxes and databind them at runtime. I have a template which consists of a checkbox and a label that holds the "text" of the combo box. Here is the template definition:

    class GroupTopicTemplate : ITemplate 
    { 
        public void InstantiateIn(Control container) 
        { 
            HtmlGenericControl div = new HtmlGenericControl("div"); 
            CheckBox chkSelected = new CheckBox(); 
            chkSelected.ID = "chkSelected"
            int rowNumber = int.Parse(container.Parent.ClientID.Substring(container.Parent.ClientID.Length - 1)) + 1; 
            chkSelected.Attributes.Add("onclick""onCheckBoxClick(" + rowNumber.ToString() + ")"); 
            div.Controls.Add(chkSelected); 
            Label lblName = new Label(); 
            lblName.Text = "Text"
            lblName.DataBinding += new EventHandler(lblName_DataBinding); 
            lblName.ID = "lblName"
            container.Controls.Add(div); 
        } 
 
        void lblName_DataBinding(object sender, EventArgs e) 
        { 
            Label target = (Label)sender; 
            RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer; 
            string itemText = (string)DataBinder.Eval(item, "Text"); 
            target.Text = itemText; 
        } 
    } 

The combo box is created in Page_Init as follows:
        RadComboBox rcGroupTopics = new RadComboBox(); 
        rcGroupTopics.ID = "rcGroupTopics" + rowString; 
        rcGroupTopics.EmptyMessage = "None"
        rcGroupTopics.HighlightTemplatedItems = true
        rcGroupTopics.AllowCustomText = true
        rcGroupTopics.Skin = "WebBlue"
        rcGroupTopics.ItemTemplate = new GroupTopicTemplate(); 
        td.Controls.Add(rcGroupTopics); 

The combo box is populated as follows:
DataView dv = new DataView(dt); 
 
dv.Sort = "name"
 
TopicDropDownList.DataSource = dv; 
TopicDropDownList.DataTextField = "name"
TopicDropDownList.DataValueField = "tagname"
TopicDropDownList.DataBind() 

I tried to put the above code in Page_Init (below the above code) and in Page_Load. The problem is, InstantiateIn() is not called until DataBind(), so the data binding event handler is attached too late and the label has no text. Is there a way around this?

Thanks in advance,
Boris Zakharin

3 Answers, 1 is accepted

Sort by
0
Accepted
Yana
Telerik team
answered on 11 Jan 2010, 02:56 PM
Hi Boris,

First, you've missed to add the label to to div in the template:

Label lblName = new Label();
lblName.Text = "Text";
lblName.DataBinding += new EventHandler(lblName_DataBinding);
lblName.ID = "lblName";
div.Controls.Add(lblName);
container.Controls.Add(div);

and also you should set the template in the OnInit event handler:

RadComboBox rcGroupTopics = new RadComboBox();
rcGroupTopics.ItemTemplate = new GroupTopicTemplate();

Best regards
Yana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Boris
Top achievements
Rank 1
answered on 11 Jan 2010, 03:15 PM
Sorry, figured it out before seeing your post. Thank you. I acknowledge your answer as correct.

Why is OnInit any better than Page_Init?
0
Yana
Telerik team
answered on 12 Jan 2010, 11:38 AM
Hello Boris,

You can use Page_Init event as well,  just make sure that the template is instantiated before RadComboBox items are initialized.

Best regards,
Yana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
Boris
Top achievements
Rank 1
Answers by
Yana
Telerik team
Boris
Top achievements
Rank 1
Share this question
or