asp.nettelerikasp.net-ajaxwebformstelerik-radlistbox

Show RadListBoxItem Without Template


I have the following Telerik RadListBox, with a template.

<tel:RadListBox ID="ToList" runat="server" DataValueField="Key" EmptyMessage="No Items" style="width:300px;">
    <ItemTemplate>
        #<asp:Literal runat="server" Text='<%# Eval("AcctNumber") %>' />
        <div>
            <asp:Literal runat="server" Text='<%# Eval("Amount", "{0:C2}") %>' />
        </div>
    </ItemTemplate>
</tel:RadListBox>

However, I'd like to insert a blank "- Select -" item at the beginning. I tried adding an item like:

ToList.Items.Insert(0, new RadListBoxItem("- Select -", ""));

However, this adds an item using the given template as the user interface; I'd like it ot use my text. Is this possible to do?


Solution

  • Luckily, there is an easy solution. Put an inline conditional statement in the ItemTemplate, as follows:

    <ItemTemplate>
    <ul>
        <li class="col1"><%# Container.DataItem != null ? DataBinder.Eval(Container.DataItem, "CompanyName") : DataBinder.Eval(Container, "Text") %></li>
        <li class="col2"><%# DataBinder.Eval(Container.DataItem, "City") %></li>
        <li class="col3"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></li>
    </ul>
    </ItemTemplate>
    <Items>
       <telerik:RadComboBoxItem Text="Select a country" />
    </Items>
    

    http://www.telerik.com/help/aspnet-ajax/combobox-insert-default-item-when-databinding.html