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

Can't get Text value of items in ItemTemplate

2 Answers 197 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 05 Nov 2010, 11:26 PM
I am attempting to get the ItemTemplate functionality to work.

I am using this code:
<telerik:RadComboBox ID="WeekEndingCB" Font-Size="Large" runat="server" OnSelectedIndexChanged="WeekEndingCB_SelectedIndexChanged"
    OnTextChanged="WeekEndingCB_TextChanged" AutoPostBack="True" Style="width: 7.2em;">
    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
    <ItemTemplate>
        Text: "<%# Eval("Text") %>"
    </ItemTemplate>
    <Items>
        <telerik:RadComboBoxItem Value="10/3/2010" Text="10/3/2010" />
        <telerik:RadComboBoxItem Value="10/10/2010" Text="10/10/2010" />
    </Items>
</telerik:RadComboBox>

I also tried DataBinder.Eval(Container.DataItem, "Text") and DataBinder.Eval(Container, "Text") as was instructed in a documentation article.

Either way, the Text of the items are not displayed in the drop down section when I click on the control.

The contents of the drop-down section are displayed as follows:
Text: ""
Text: ""

The expected contents would be:
Text: "10/3/2010"
Text: "10/10/2010"

The text of the selected item IS displayed as the selected item when an item is selected.

The control behaves as expected if I remove the ItemTemplate section.

How can I get the templates to work as expected?

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Nov 2010, 05:57 AM
Hello Andy ,
To set the Text in the template, use a DataBinder.Eval statement:

ASPX:
<ItemTemplate>
     Text: "<%# DataBinder.Eval(Container, "Text")%> "
</ItemTemplate>
<Items>
    <telerik:RadComboBoxItem Value="10/3/2010" Text="10/3/2010" />
    <telerik:RadComboBoxItem Value="10/10/2010" Text="10/10/2010" />
</Items>
 
In addition to adding a DataBinder.Eval statement, you must also explicitly call the DataBind method for the items so that the template has access to the Text property:

C#:
protected void Page_Load(object sender, EventArgs e)
   {
       for (int i = 0; i < RadComboBox1.Items.Count; i++)
       {
           RadComboBox1.Items[i].DataBind();
       
 
   }


Thanks,
Princy.
0
Andy
Top achievements
Rank 1
answered on 08 Nov 2010, 07:38 PM
Thanks, this worked.

How come I have to use DataBinder.Eval for this, while Eval by itself works in other binding situations? (Like in RadGrid columns?)
Tags
ComboBox
Asked by
Andy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or