Issue with generating RadCombobox ItemTemplate dynamically

1 posts, 0 answers
  1. khashayar
    khashayar avatar
    1 posts
    Member since:
    Feb 2017

    Posted 01 Feb Link to this post

    Hello Telerik Team
    I have a usercontrol.ascx in which I have put a RadComboBox.
    I am trying to generate the RadComboBoxItems and HeaderTemplate and ItemTemplate dynamically.
    I have succeded in generating the HeaderTemplate.
    But I have an issue in ItemTemplate generation.
    I have tried the following Code in the usercontrol.ascx where ‘drpLookup’ is the RadComboBox ID:

    protected override void OnPreRender(EventArgs e)
            {
                if (!IsPostBack)
                {
                    drpLookup.Attributes.Add("LookupType", LookupType);
                    drpLookup.Attributes.Add("ServiceClass", ServiceClass);
                }
                HeaderTemplate.ServiceClass = drpLookup.Attributes["ServiceClass"];
                ItemTemplate.ServiceClass = drpLookup.Attributes["ServiceClass"];
                base.OnPreRender(e);
            }
    protected override void OnInit(EventArgs e)
            {
                drpLookup.HeaderTemplate = new HeaderTemplate();
                drpLookup.ItemTemplate = new ItemTemplate();
                base.OnInit(e);
            }
    protected void drpLookup_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
            {
                         Datatable dt=//some code to get data
                         BindDrpLookup(dt, properties, DataValueFieldName, DataTextFieldName);
            }
    private void BindDrpLookup(DataTable dataTable, List<PropertyInfo> properties, string DataValueFieldName, string DataTextFieldName)
            {
                drpLookup.DataValueField = DataValueFieldName;
                drpLookup.DataTextField = DataTextFieldName;

                foreach (DataRow l in dataTable.Rows)
                {
                    RadComboBoxItem item = new RadComboBoxItem();

                    foreach (var p in properties)
                    {
                        item.Attributes.Add(p.Name, l[p.Name].ToString());
                    }
                    drpLookup.Items.Add(item);
      item.DataBind();
                }
                drpLookup.DataBind();

            }


    private class ItemTemplate : ITemplate
            {
                public static string ServiceClass { get; set; }
                public void InstantiateIn(Control container)
                {
                    PropertyInfo propertyWithLookupValue =//Do Some Code To get values

                    HtmlTable table = new HtmlTable();
                    table.Width = "100%";
                    table.CellPadding = 0;
                    table.CellSpacing = 0;
                    HtmlTableRow row = new HtmlTableRow();
                    row.Style.Add("width", "100%");
                    int numberofColumns = propertiesWithLookup.Count - 1;
                    string ColumnWidth = Math.Round((decimal)(100 / numberofColumns)).ToString();
                    foreach (PropertyInfo p in propertiesWithLookup)
                    {
                        if (p != propertyWithLookupValue)
                        {
                            HtmlTableCell cell = new HtmlTableCell();
                            cell.Width = ColumnWidth + "%";
                            cell.ID = p.Name;
                            cell.DataBinding += new EventHandler(cell_DataBinding);
                            row.Controls.Add(cell);
                        }
                    }
                    table.Controls.Add(row);
                    container.Controls.Add(table);
                }

                private void cell_DataBinding(object sender, EventArgs e)
                {
                    HtmlTableCell target = (HtmlTableCell)sender;
                    RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;
                    string[] id = target.ClientID.Split('_');
    string itemText = (string)DataBinder.Eval(item, id[id.Length - 1].ToString());
                    target.InnerText = itemText;
                }
            }



    The issue is that in the cell_DataBinding event at the following line of Code:
    string itemText = (string)DataBinder.Eval(item, id[id.Length - 1].ToString());


    I get an exception with the following Error message:
    {"DataBinding: 'Telerik.Web.UI.RadComboBoxItem' does not contain a property with the name 'Name'."}

    And following stacktrace:

       at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
       at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts)
       at System.Web.UI.DataBinder.Eval(Object container, String expression)
       at APA.UI.Web.APAAdmin.Views.UserControls.Lookup.ItemTemplate.cell_DataBinding(Object sender, EventArgs e) in D:\Projects\Framework\2015\APA\APA.UI.Web.APAAdmin\Views\UserControls\Lookup.ascx.cs:line 217
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()



    any helps will be very much appreciated !
Back to Top