Can't get Text value of items in ItemTemplate

3 posts, 0 answers
  1. Andy
    Andy avatar
    10 posts
    Member since:
    Apr 2009

    Posted 05 Nov 2010 Link to this post

    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. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Nov 2010 Link to this post

    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.
  3. Andy
    Andy avatar
    10 posts
    Member since:
    Apr 2009

    Posted 08 Nov 2010 Link to this post

    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?)
Back to Top