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