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

RadCombobox and dynamic ItemTemplate

2 Answers 358 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Osman
Top achievements
Rank 1
Osman asked on 23 Feb 2009, 12:29 PM

Hi all,

I'm trying to use and customize my radCombox. I created a custom template class which implements ITemplate in order to create multi-columns (4 cols) combo. All have to be done dynamically in the code-behind... I use Internet Explorer 7, and the version of Telerik.Web.UI is 2008.3.1105.20.

First of all I initialize the templates in the Init() method of the page and set them to the combobox.
After I just bind a collection objects to the combobox.
The result is that the headerTemplate table is well sized, its width takes 100% of the container. In the ItemTemplate, the size of the table is not well sized because I suppose the table is contained in ul and li tag which generated by radcombobox.
 
How can I created a table dynamically and set its width to 100% in itemTemplate? Does anyone have an idea?
Find below a little portion of code that may help you...

protected void Page_Init(object sender, EventArgs e)

{

    HeaderTemplate headertemplate = new HeaderTemplate();

    ItemTemplate itemtemplate = new ItemTemplate();

    this.cbxLocation.HeaderTemplate = headertemplate;

    this.cbxLocation.ItemTemplate = itemtemplate;

}

 

public class HeaderTemplate : ITemplate

{

    #region ITemplate Members

        public void InstantiateIn(Control container)

        {

            HtmlTable table = new HtmlTable();

            table.Width = Unit.Percentage(100).ToString();

            HtmlTableRow row = new HtmlTableRow();

            HtmlTableCell cell;

            for (int i = 1; i <= 4; i++)

            {

                cell = new HtmlTableCell();

                cell.Width = Unit.Percentage(100 / 4).ToString();

                switch (i)

                {

                    case 1: cell.InnerText = "Column 1";

                                break;

                    case 2: cell.InnerText = "Column 2";
                                break
;

                    case 3: cell.InnerText = "Column 3";

                                break;

                    case 4: cell.InnerText = "Column 4";

                                break;

               }

               row.Controls.Add(cell);

            }

            table.Controls.Add(row);

            container.Controls.Add(table);

    }

    #endregion

}


 

 

public class ItemTemplate : ITemplate

 

 

 

{

 

 

 

 

#region

ITemplate Members

 

 

    public void InstantiateIn(Control container)

 

    {

 

        HtmlTable table = new HtmlTable();

 

        table.Width =

Unit.Percentage(100).ToString();

 

 

        HtmlTableRow row = new HtmlTableRow();

 

 

        HtmlTableCell cell;

 

 

        for (int i = 1; i <= 4; i++)

 

        {

                cell =

new HtmlTableCell();
                cell.Width = Unit.Pixel(100 / 4).ToString();

 

 

                switch (i)

 

                {

 

                     case 1:

 

                         cell.InnerText = "Data 1";

                         break;

 

 

                    case 2:

 

                         cell.InnerText = "Data 2";

                        break;

 

 

                    case 3:

 

                         cell.InnerText = "Data 3";

                        break;

 

 

                    case 4:

 

                         cell.InnerText = "Data 4";

                        break;

 

                }

                row.Controls.Add(cell);

        }

        table.Controls.Add(row);

        container.Controls.Add(table);

    }

 

 

#endregion


}

 

Thanks for your help

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Yana
Telerik team
answered on 26 Feb 2009, 08:37 AM
Hello Osman,

This is the right way to create dynamically table, the problem is that when you set width of the cells in ItemTemplate, you set it as Pixels instead of Percentages:

cell.Width = Unit.Pixel(100 / 4).ToString();  

should be like in the Header Template:

cell.Width = Unit.Percentage(100 / 4).ToString(); 

All the best,
Yana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Osman
Top achievements
Rank 1
answered on 05 Mar 2009, 10:50 AM
Thanks a lot!

It worked :)
Tags
ComboBox
Asked by
Osman
Top achievements
Rank 1
Answers by
Yana
Telerik team
Osman
Top achievements
Rank 1
Share this question
or