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

ComboBox and ITemplate fails under ASP.NET AJAX (working under ASP.NET)

1 Answer 50 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 27 Aug 2008, 08:50 PM

Telerik.Web.UI.dll v2008.1.619.20

Hi Telerik, trying to upgrade my custom (inherited) ComboBox to the version above from RadComboBox.Net2.dll v2.8.1.0

Inheriting from Combox, ComboBoxItem and creating a custom ItemTemplate (note that ComboBoxItem in this example is not really necessary but my production version has additional attributes added here)

Basically, when I 'databind my control it renders fine, but when I add elements to my cmbo via the <items/> collection in my ASPX page the controls fails to render.  I added calls to .DataBind() on the control and the individual items but this doesn't seem to help any ...debugger shows that the ComboBoxTemplate is never instantiated/created for the controls with items declared in the markup.

All was/is fine under the old version?

Thanks for any and all advice

ComboBox.cs

using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
namespace TelerikTestSite 
    public class ComboBox : RadComboBox 
    { 
        protected override void OnInit ( EventArgs e ) 
        { 
            // set template 
            base.ItemTemplate = new ComboBoxTemplate ( ); 
 
            // continue event 
            base.OnInit ( e ); 
        } 
    } 

ComboBoxItem.cs

namespace TelerikTestSite 
    public class ComboBoxItem : RadComboBoxItem 
    { 
        public ComboBoxItem ( ) : base ( ) 
        { 
 
        } 
    } 

ComboBoxTemplate.cs

using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
namespace TelerikTestSite 
    public class ComboBoxTemplate : ITemplate 
    { 
        public void InstantiateIn ( Control container ) 
        { 
            CheckBox cb = new CheckBox ( ); 
            cb.ID = "cb"
            cb.DataBinding += new EventHandler ( cb_DataBinding ); 
            container.Controls.Add ( cb ); 
        } 
 
        void cb_DataBinding ( object sender, EventArgs e ) 
        { 
            CheckBox target = (CheckBox) sender; 
            RadComboBoxItem item = (RadComboBoxItem) target.BindingContainer; 
            target.Text = (string) DataBinder.Eval ( item, "Text" ); 
        } 
    } 

Default.aspx

<div> 
            DataBind ..works: <app:combobox runat="server" id="cb1" /> 
        </div> 
        <br /> 
        <div>Declarative ..fails: 
            <app:combobox runat="server" id="cb2" highlighttemplateditems="true"
                <items> 
                    <app:comboboxitem text="Item 1" /> 
                    <app:comboboxitem text="Item 2" /> 
                    <app:comboboxitem text="Item 3" /> 
                </items> 
            </app:combobox> 
        </div> 

Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e) 
    { 
        ArrayList al = new ArrayList ( 3 ); 
        al.Add ( "Item 1" ); 
        al.Add ( "Item 2" ); 
        al.Add ( "Item 3" ); 
 
        this.cb1.DataSource = al; 
        this.cb1.DataBind ( ); 
 
        foreach ( ComboBoxItem item in cb2.Items ) 
            item.DataBind ( ); 
 
        this.cb2.DataBind ( ); 
    } 




1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 01 Sep 2008, 12:06 PM
Hello Matthew Tamm,

Please try the following:

ComboBoxTemplate  template = new ComboBoxTemplate (); 
   foreach (RadComboBoxItem item in RadComboBox1.Items) 
   {             
       template.InstantiateIn(item); 
   } 
    
   RadComboBox1.DataBind(); 

Let me know the results.

All the best,
Veskoni
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
ComboBox
Asked by
Brandon
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Share this question
or