Add item template to radcombobox dynamically

4 posts, 0 answers
  1. Neema
    Neema avatar
    3 posts
    Member since:
    Jun 2010

    Posted 24 Jun 2010 Link to this post

    Hi,

    I have binded some data to radcombobox during pageload. This is rendered as multicolumned radcomboxbox. I need to add some more data dynamically after user inputs the value.

    Please let me know how to achieve ths.
    The below is what I have implemented but the data is not getting inserted in the dropdow

    public

     

    class ItemTemplate : ITemplate

     

    {

     

    public void InstantiateIn(Control container)

     

    {

     

    Literal lit = new Literal();

     

    lit.Text =

    "<div class='cmb_rdFnt cmb_ltFlt'>" + "ABC" + "</div> <div class='cmb_rdFnt cmb_ltFlt cmb_sp20'>"+"DEF"+"</div>"+"<div class='cmb_rdFnt cmb_ltFlt cmb_sp20'>"+"GHI"+"</div>";

     

    container.Controls.Clear();

    container.Controls.Add(lit);

    }
    // Page_load evenet

     

    if

     

    (!Page.IsPostBack)

     

    {

     

    PaymentSelectionData pmtSelData;

     

     

    for (int i = 0; i < 5; i++)

     

    {

    pmtSelData =

    new PaymentSelectionData();

     

    pmtSelData.paymentTypeDesc =

    "desc1" + i.ToString();

     

    lstPmtSel.Add(pmtSelData);

    }

    RadComboBox1.DataValueField =

    "refVal";

     

    RadComboBox1.DataTextField =

    "paymentTypeDesc";

     

    RadComboBox1.DataSource = lstPmtSel;

    RadComboBox1.DataBind();

     

    }

     

     

     

     

     

    else

     

     

     

     

     

    {

     

     

     

     

     

    RadComboBoxItem item = new RadComboBoxItem();

     

    item.Attributes.Add(

    "info", "Hello");

     

    RadComboBox1.Items.Add(item);

     

    ItemTemplate i1 = new ItemTemplate();

     

    i1.InstantiateIn(item);

    RadComboBox1.DataBind();

     

    }

  2. Kevin
    Kevin avatar
    2 posts
    Member since:
    Jun 2010

    Posted 24 Jun 2010 Link to this post

    First off, you have to set the ItemTemplate in the Page_Init event of the page. Like so:

    protected void Page_Init(object sender, EventArgs e) 
        { 
            ddlSubListing.ItemTemplate = new ItemTemplate(); 
        } 

    Also, to get the data content to display you need to attach a DataBinding event handler to your Literal control. Like so:

    public void InstantiateIn(Control container) 
        { 
            Literal lit = new Literal(); 
            lit.DataBinding += new EventHandler(lit_DataBinding); 
     
            container.Controls.Add(lit); 
        } 
     
        void lit_DataBinding(object sender, EventArgs e) 
        { 
            Literal target = (Literal)sender; 
            RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer; 
            string itemText = (string)DataBinder.Eval(item, "Text"); 
            target.Text = itemText; 
        } 

    I hope that helps.
  3. Neema
    Neema avatar
    3 posts
    Member since:
    Jun 2010

    Posted 25 Jun 2010 Link to this post

    Thank you. After including data inding it is working fine, but I am facing another issue

    During page load, I have binded the data at run time.
    Eg; I had the data in a dataset and I have done
    Radcombobox1.DataSource = objDataSet;
    Radcombobox1.DataBind().

    I had set the itemtemplate at desgin time. The radcombo was rendered such that there were say 3 columns

    Now, on buttonclick, I have to add new data with 3 columns to the existig data.

    For this, I have implemented the below lines of code(Taken from one of the telerik forums)

     

     

    for (int i = 0; i < 5; i++)

     

    {

     

    RadComboBoxItem item = new RadComboBoxItem("item " + i.ToString());

     

    item.Attributes[

    "col2"] = "col2_" + i.ToString();

     

    item.Attributes[

    "col3"] = "col3_" + i.ToString();

     

    RadComboBox1.Items.Add(item);

    }

     

     

     

     

    ItemTemplate template = new ItemTemplate();

     

     

    foreach (RadComboBoxItem it in RadComboBox1.Items)

     

    {

    template.InstantiateIn(it);

    }

    RadComboBox1.DataBind();



     

    class

     

    ItemTemplate : ITemplate

     

     

     

     

    {

     

    public void InstantiateIn(Control container)

     

    {

     

    Table table = new Table();

     

    table.Width =

    Unit.Percentage(100);

     

     

    TableRow mainRow = new TableRow();

     

     

    TableCell cell1 = new TableCell();

     

    cell1.DataBinding +=

    new EventHandler(cell1_DataBinding);

     

    mainRow.Cells.Add(cell1);

     

    TableCell cell2 = new TableCell();

     

    cell2.DataBinding +=

    new EventHandler(cell2_DataBinding);

     

    mainRow.Cells.Add(cell2);

     

    TableCell cell3 = new TableCell();

     

    cell3.DataBinding +=

    new EventHandler(cell3_DataBinding);

     

    mainRow.Cells.Add(cell3);

    table.Rows.Add(mainRow);

    container.Controls.Add(table);

    }

     

    private void cell1_DataBinding(object sender, EventArgs e)

     

    {

     

    TableCell target = (TableCell)sender;

     

     

    RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;

     

    target.Text = (

    string)DataBinder.Eval(item, "Text");

     

    }

     

    private void cell2_DataBinding(object sender, EventArgs e)

     

    {

     

    TableCell target = (TableCell)sender;

     

     

    RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;

     

    target.Text = (

    string)DataBinder.Eval(item, "Attributes[\"col2\"]");

     

    }

     

    private void cell3_DataBinding(object sender, EventArgs e)

     

    {

     

    TableCell target = (TableCell)sender;

     

     

    RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;

     

    target.Text = (

    string)DataBinder.Eval(item, "Attributes[\"col3\"]");

     

    }

    }



    The issue which I am facing is that new data has been added with 3 columns, but I see only one column of the existing data,

    ANy help please

  4. Yana
    Admin
    Yana avatar
    4890 posts

    Posted 01 Jul 2010 Link to this post

    Hello Neema,

    I've attached a simple page based on your code to demonstrate how you can achieve the needed approach, please download it and give it a try.

    Regards,
    Yana
    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
Back to Top