[Solved] Listbox ItemTemplate not showing Attributes data

1 Answer 3 Views
ListBox
Fit2Page
Top achievements
Rank 3
Bronze
Bronze
Iron
Fit2Page asked on 14 Oct 2026, 09:11 AM

Hi,

 

I have a Listbox with sqldatasource

    <telerik:RadListBox RenderMode="Lightweight" runat="server" ID="RadListBox2" OnInserted="RadListBox2_Inserted" DataSourceID="sds_items" OnItemDataBound="RadListBox2_ItemDataBound" DataTextField="NLdescription" DataValueField="Supplier" DataKeyField="id" Width="100%" AllowReorder="true"  
                     AllowTransferDuplicates="true" Skin="Default">
                    <itemtemplate>
                        <div class="container border-bottom py-2">
                            <div class="font-weight-bold">Type: <%# DataBinder.Eval(Container, "Value")%></div>
                        <div class="font-weight-bold"><asp:TextBox ID="tb" Text='<%# DataBinder.Eval(Container, "Text")%>' runat="server"></asp:TextBox></div>

                                <label>Number:</label>
                                <span><%# DataBinder.Eval(Container, "Attributes['NLvar1']") %></span>

                                <label>Text:</label>

                           </div>
                    </itemtemplate>
                </telerik:RadListBox>

 

And in Code Behind:

 

    Protected Sub RadListBox2_ItemDataBound(sender As Object, e As RadListBoxItemEventArgs)

        Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
        e.Item.Attributes.Add("NLvar1", drv("NLvar1").ToString())

    End Sub       

 

The Listbox is not showing any data for the NLvar1 attribute...what am I missing?

                                                                        

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 14 Oct 2026, 10:12 AM

Hi Marc,

The issue occurs because the <%# DataBinder.Eval(Container, "Attributes['NLvar1']") %> expression in your ItemTemplate is evaluated during the server-side data binding phase. At this point, the Attributes collection of the RadListBoxItem is not yet populated with the "NLvar1" value, since this is set later in the ItemDataBound event. As a result, the value is not available when the template is rendered.

Why the Attributes Collection Doesn't Work Here

  • The server-side data binding expressions in the template are processed before the ItemDataBound event is fired.
  • The e.Item.Attributes.Add("NLvar1", ...) line sets the attribute after the template is already rendered, so the attribute value cannot be accessed in the template using data binding.

Solution

To display the "NLvar1" value in your ItemTemplate, bind it directly from the data source, not from the item's Attributes collection. You can use the following approach:

<span><%# DataBinder.Eval(Container.DataItem, "NLvar1") %></span>

or, in VB syntax:

<span><%# Eval("NLvar1") %></span>

Updated ItemTemplate Example

<ItemTemplate>
    <div class="container border-bottom py-2">
        <div class="font-weight-bold">Type: <%# DataBinder.Eval(Container, "Value") %></div>
        <div class="font-weight-bold">
            <asp:TextBox ID="tb" Text='<%# DataBinder.Eval(Container, "Text") %>' runat="server"></asp:TextBox>
        </div>
        <label>Number:</label>
        <span><%# Eval("NLvar1") %></span>
        <label>Text:</label>
    </div>
</ItemTemplate>
The Attributes collection is useful for storing additional values for client-side access or later use in code-behind, but it is not suitable for direct binding in the ItemTemplate.


If you need to access these attributes on the client side (e.g., via JavaScript), you can continue to add them in the ItemDataBound event, and they will be rendered as HTML attributes in the ListBox item's markup.

    Regards,
    Rumen
    Progress Telerik

    Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources! 
    Tags
    ListBox
    Asked by
    Fit2Page
    Top achievements
    Rank 3
    Bronze
    Bronze
    Iron
    Answers by
    Rumen
    Telerik team
    Share this question
    or