+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / RadComboBox ItemTemplate Not Binding

RadComboBox ItemTemplate Not Binding

14 posts, 0 answered
  1. Richard Baugh
    Richard Baugh avatar
    201 posts
    Registered:
    22 Aug 2012
    05 Mar 2010
    Link to this post
    I've got a control that inherits from the SimpleControl. This control has a RadComboBox in the template and is bound to an ObjectDataSource. The RadComboBox uses the ItemTemplate and I cannot get the data to bind/output. This control is used as a MetaField control for a Generic Content module. If I select an item, then the combo box's text value is set you I can tell which I selected, I just can't get the data to bind to the ItemTemplate so that it shows in the drop down list.

    I found a few articles that mention having to bind the actual items which I have tried. I have put the code, to loop through all items and bind them, in the OnPreRender and even in the ItemDataBound event for the combo box. Each results in the following error when trying to bind the data using either DataBinder.Eval(Container, "Text") and DataBinder.Eval(Container.DataItem, "Text"):
    [ArgumentNullException: Value cannot be null.
    Parameter name: expression]
    

    I'm not having any luck with the templates. I am nor sure if this has anything to do with the Sitefinity Parser since I am using the SimpleControl class and an embedded template or if there is another underlying problem.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Mar 2010
    Link to this post
    Hello Richard Baugh,

    The problem is not in the binding of RadComboBox. When inherit from Simple control the ChildControls are cleared and there is no value set to your control when you edit a content item. This also results in the binding of your combo, but it really depends on where and how you bind the control. To make the long story short you will have problems with editing an item when you use custom control that inherits from SimpleControl to populate a metakey.

    You can

    1. Inherit from CompositeControl, ITextControl and create your controls dynamically without using a template for your control. Here you can override CreateChildControls() and call Controls.Add() to add your custom control to the collection.

    public class SampleControl: CompositeControl, ITextControl
     {
      public SampleControl()
      {
      }
     
      protected override void CreateChildControls()
      {
          this.Controls.Add(Combo);
      }
      
     
     
     
     private RadComboBox Combo
      {
          get
          {
              if (this._combo == null)
                  this._combo = new RadComboBox();
              return this._combo;
          }
      }
     
      private RadComboBox _combo;
    }


    2. Create a class that inherits from CompositeControl, ITextControl and implement your own container and instinate your custom template.

    3. When you edit the item get the item id from the QueryString  Param and use Manager.GetContent(PASS THE ID HERE). Then, use GetMetaData to retrieve the value.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    12 Apr 2010
    Link to this post
        RadComboBox RadDropDownShopper1 = new RadComboBox();
                            RadDropDownShopper1.ID = "RadDropdownShopper1" + i.ToString();
                            RadDropDownShopper1.Attributes.Add("style", "padding-top: 8px;");
                            RadDropDownShopper1.Attributes.Add("style", "padding-left: 30px;");
                            RadDropDownShopper1.ItemTemplate = new TextBoxTemplate();
                            RadDropDownShopper1.DataSource = FakeDataTable();
    RadDropDownShopper1.DataBind();
      
    --------------
      
      
     class TextBoxTemplate : ITemplate
        {
            string ColName = "Value";
      
            public void InstantiateIn(Control container)
            {
                TextBox textBox = new TextBox();
                textBox.ID = "tabTextBox";
                textBox.DataBinding += new EventHandler(this.txt_DataBind);
                //textBox.Text = DataBinder.Eval( "'<%= Eval(\"Value\") %>'";
                container.Controls.Add(textBox);
            }
            public void txt_DataBind(Object sender, EventArgs e)
            {
                TextBox txt = (TextBox)sender;
                RadComboBoxItem radCombo = (RadComboBoxItem)txt.NamingContainer;
                //DataRowView dv = (DataRowView) DataBinder.Eval(radCombo, "Value");
                //txt.Text = dv.Row["Value"].ToString();
                txt.Text = DataBinder.Eval(radCombo, ColName).ToString();
            }
        }

    Hi Ivan ,
            I created the template with textbox and filling the text.. Its not binding the value.. instead just displaying System.Data.DataRowView in textbox.. What am doing wrong? How can i resolve that..

    Am creating rad combo and template dynamically from code behind..

    Thanks
    nimmi
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Apr 2010
    Link to this post
    Hi Esther Nirmala,

    The row below returns you the data object instead of its text. You could attach the debugger to this line to see what actually is returned by your data source.

    txt.Text = DataBinder.Eval(radCombo, ColName).ToString();

    The first parameter of the Eval method should be the DataItem, the second is the key.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    12 Apr 2010
    Link to this post
    public static DataTable FakeDataTable()
           {
               DataTable dataTable = new DataTable();
               dataTable.TableName = "FakeTable";
               dataTable.Columns.Add("Text");
               dataTable.Columns.Add("Value");
               dataTable.Columns.Add("ID");
               dataTable.Columns.Add("ParentID");
               dataTable.Rows.Add(new object[] { "Root 1", "Value 1", 1, null });
               dataTable.Rows.Add(new object[] { "Root 2", "Value 2", 2, null });
               dataTable.Rows.Add(new object[] { "Child 1.1", "Value 3", 3, 1 });
               dataTable.Rows.Add(new object[] { "Child 1.2", "Value 4", 4, 1 });
               dataTable.Rows.Add(new object[] { "Child 1.1", "Value 3", 5, 2 });
               dataTable.Rows.Add(new object[] { "Child 1.2", "Value 4", 6, 2 });
               return dataTable;
           }

    Hi Ivan,
            This is my datatable.. From this am attaching the Value Column.. But txt.Text = DataBinder.Eval(radCombo, ColName).ToString(); - returns "System.Data.DataRowView" string.. I dont know whats wrong with this.. I think am not doing anything wrong.. radcombo is a radcomboDataItem, colName is the fieldName..

    Thanks
    Esther
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Apr 2010
    Link to this post
    Hello Esther Nirmala,

    Have you attache the code to VS debugger? It returns System.Data.DataRowView for the text of the TextBox. You should use the DataItem as suggested

    txt.Text = DataBinder.Eval(radCombo.DataItem, ColName).ToString();

    You could do the same for the RadComboBox default value.

    The template of the RadComboBox  should be created OnInit.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    12 Apr 2010
    Link to this post
    Hi Ivon,

            Thank you so much for your reply in time.. Its working Perfectly .. I just changed the "radCombo" to "radCombo.DataItem." Thanks again.

    Regards,
    Esther.
  8. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    13 Apr 2010
    Link to this post
    Hi Ivon,

            I want to validate the checkbox in the radcombo.. It should have atleast one selected item. In this code 
      var itemDiv = document.getElementById(item.ClientID);   this returns null.. item returns the object.. but couldnt get items.ClientID.. How to validate atleast once checkbox is checked or not in rad combo in javascript.

    Thanks,
    Nimmi


    function TestRadDontSelect(Obj, dont_sel_index, err) {
        var combobox = $find(Obj);
        var items = combobox.get_items();
        var itemsCount = items._array.length;
      
        for (var itemIndex = 0; itemIndex < itemsCount; itemIndex++) {
            var item = items._array[itemIndex];
      
            var checkbox = getItemCheckBox(item);
      


            if (checkbox) {
                //Check whether the Item's CheckBox) is checked.  
                if (checkbox.checked) {
                    //Perform necessary operations...  
                }
                /* 
                else 
                
                //Perform necessary operations... 
                
                */
            }
        }
    }     
    function getItemCheckBox(item)  
            {  
                //Get the 'div' representing the current RadComboBox Item.  
                var itemDiv = document.getElementById(item.ClientID);  
                    
                //Get the collection of all 'input' elements in the 'div' (which are contained in the Item).  
                var inputs = itemDiv.getElementsByTagName("input");  
                    
                for (var inputIndex = 0; inputIndex < inputs.length; inputIndex++)  
                {     
                    var input = inputs[inputIndex];  
                        
                    //Check the type of the current 'input' element.  
                    if (input.type == "checkbox")  
                    {  
                        return input;  
                    }  
                }  
                    
                return null;  
            }  
               
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Apr 2010
    Link to this post
    Hello Esther Nirmala,

    You need to find the control from the RadCombobox template

    var combo = $find("<%= RadComboBox1.ClientID %>");
    var mycontrol = combo.get_items().getItem(0).findControl("mycontrol1");
    // or
     
    var tree = $find('<%= RadComboBox1.Items[0].FindControl("mycontrol1").ClientID %>');


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    13 Apr 2010
    Link to this post
    Hi Ivon,

    I tried with both options.. its not working.. can u send a sample..?

    var

     

    combobox = $find(Obj); - its returning the object.. My problem is to fetch the check box items from that radcombo and to check whether it is checked or not.. am not at all getting  the check box items..

     


    I got the items of rad combo using

     

    var items = combobox.get_items();

     

     

    var itemsCount = items._array.length;

     


    but finding the checkbox i couldnt use the below approach..

     

    var tree = $find('<%= RadDropdownShopper1.Items[0].FindControl("checkBoxShopper").ClientID %>');

     

     

    var mycontrol = combobox.get_items().getItem(0).findControl("checkBoxShopper");

     


    is their anyother way to achieve the same ..?

    Thanks
    Nimmi
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Apr 2010
    Link to this post
    Hi Esther Nirmala,

    Here is a sample code that gets the first CheckBox of the dynamically created template with CheckBox control.

    <script type="text/javascript">
        function OnClientLoad(sender) {
            var combo = sender;
            var item = combo.get_items().getItem(0);
            var ns = item._element.firstChild.nextSibling;
     
            if (ns != null) {
                // here check the item explicitly so we will alert true
                ns.checked = true;
                var getID = item._element.firstChild.nextSibling.id
                if (getID != null) {
                    alert(getID);
                    alert(ns.checked);
                }
            }
        }
    </script>


    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  12. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    13 Apr 2010
    Link to this post
    Hi Ivan,

            Thanks. I got it working. But after postback, the checkbox is not retaining its text .. but check box is visible.. But It's binding again.. I  have attached the portion of my screenshot. Whats the root cause of this issue..?

    Thanks
    Nimmi
  13. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    15 Apr 2010
    Link to this post
    Hi Ivan,

            Am creating the textbox,radcombo with check box dynamically in code behind.. while creating the rad combo it will be loaded with values from db..  on the initial load, its loading the values properly... but on the textbox changed event it will create the controls again and bind the values.. its doing that. but on adding the controls to the place holder its calling the checkbox template of rad combo and creating the controls again but its not binding the values..

     

    class CheckBoxTemplate : ITemplate

     

    {

     

    string ColName = "FirstName";

     

     

    public void InstantiateIn(Control container)

     

    {

     

    CheckBox checkBoxShopper = new CheckBox();

     

    checkBoxShopper.ID =

    "checkBoxShopper";

     

    checkBoxShopper.Width = 100;

    checkBoxShopper.DataBinding +=

    new EventHandler(this.chkShopper_DataBind);

     

     

    container.Controls.Add(checkBoxShopper);}

     

    }

     

    public void chkShopper_DataBind(Object sender, EventArgs e)

     

    {

     

    CheckBox chkShopper = (CheckBox)sender;

     

     

    RadComboBoxItem radComboItem = (RadComboBoxItem)chkShopper.NamingContainer;

     

    chkShopper.Text =

    DataBinder.Eval(radComboItem.DataItem, ColName).ToString();

     

    }

    }



    This is my check box template.. its getting called on adding the controls to the placeholder, InstantiateIn() is called but not  chkShopper_DataBind().. thus clearing the text of check box.. but instantiateIn() is not called on adding the controls for the first time.. how to avoid this.. need the soln..

    thanks in advance

    nimmi
  14. Esther Nirmala
    Esther Nirmala avatar
    8 posts
    Registered:
    09 Apr 2010
    26 Apr 2010
    Link to this post
    Hi,

         I am creating the rad combo with check box dynamically from code behind. I want to set the initial value as "Select One" in rad combo. If I add the "Select One" by

    RadComboShopper.Items.Insert(0,

    new RadComboBoxItem(UIConfigurationConstant.SELECTONE));

     

    RadDropDownShopper.Items[0].Selected =

    true;  Its adding the items but still displaying the items in the list below.. so am setting it visibility to false by

     

     

     

    RadDropDownShopper.Items[0].Visible =

    false;.. But if i click on the item its displaying the item text as initial value(i.e., like title/empty message). But if i click perfectly in the check box it retains the select one as "initial text". how to avoid this..? in need of solution. help out ..

     

     




    Thanks
    Nimmi
Register for webinar
14 posts, 0 answered