Asp.NET ListView Data Control Dynamically Databind

13,286

Ok in answer to your questions:

  • You can iterate over the ListViewItems by using the OnItemDataBound event in the parent ListView. You can then use this to databind nested child ListViews (or insert Html or manipulate the contents of the list in any way you need). Make sure you use ListViewItemEventArgs in your code behind handler so you can access the databound item easily...
  • You can use Databinder.Eval to dynamically populate a Repeater in your ListView using something like this (note 'GetChildCategoryData' is a code behind method):

Hope it helps..

<asp:ListView ID="parentList" runat="server">
   <ItemTemplate>
      <asp:Repeater ID="childData" runat="server" DataSource='<%# GetChildCategoryData(DataBinder.Eval(Container.DataItem, "parentcategoryID")) %>'>.. </asp:Repeater>
   </ItemTemplate>
</asp:ListView>
Share:
13,286
mirezus
Author by

mirezus

Updated on June 05, 2022

Comments

  • mirezus
    mirezus almost 2 years

    I am trying to implement a ListView data control for displaying and editing lookup table/ application level variables. There are multiple entity classes which can be bound to the ListView, so the ItemTemplate needs to be dynamically bound to the selected entity object.

    For example i have:

    AddressType { AddressTypeId, AddressTypeDescription}, 
    PhoneType { PhoneTypeId, PhoneType}, 
    MarriageStatusType { MarriageStatusId, marriageStatusType}
    

    Those generated entity objects prevent me from simply doing something like the following snippet, because the ID and Type properties are different on each business object.

    <ListView>
    ...
    
    <itemTemplate>
        <tr> 
           <td runat="server" id="tdId"> <%# Eval("ID") %> </td> 
           <td runat="server" id="tdType"> <%# Eval("TypeNameDescription") %> </td> 
        </tr>
    </itemTemplate>
    
    ...
    </ListView>
    

    I am trying to discover : 1. How to iterate over the ListView Items to insert the appropriate property value into the server side html td tags. 2. How to use Databinder.Eval on the ListView items to insert that property value.

    Thanks in advance!