How to bind to a nested DataList using a property collection

atconway

I have seen (3) different examples of using a nested DataList or Repeater in ASP.NET, but all use a combination of a raw DataTable/DataSet and CreateChildView call to create the relationship between the outer and inner DataList.

Well since it is 2013 and not 2003, I have a class with the following structure:

public class Customer
{    
   public string FullName { get; set; }   
   public List<Orders> { get; set; }   
}

The idea is that the Orders collection is what is bound as the DataSource for the inner, nested DataList. The problem is I can't seem to figure out how to set up the relationship in the code for the binding. The inner DataList is actually not directly accessable in code via Intellisense since it is nested under the main DataList. Here is the typical code in the ItemDataBound event to find the inner DataList and assign its DataSource using CreateChildView:

protected void outerRep_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        DataRowView drv = e.Item.DataItem as DataRowView;
        DataList innerDataList = e.Item.FindControl("innerDataList") as DataList;
        innerDataList.DataSource = drv.CreateChildView("OrdersRelation");
        innerDataList.DataBind();
    }
}

"OrdersRelation" in all the examples I see is set up like the following:

// Attach the relationship to the dataSet
ds.Relations.Add(new DataRelation("OrdersRelation", ds.Tables[0].Columns["OrderID"],
ds.Tables[1].Columns["OrderID"]));
outerDataList.DataSource = ds.Tables[0];
outerDataList.DataBind();

Obviously this is not applicable as I'm using a strongly typed object with a collection property of 'Orders' on it.

How can I achieve the same result and bind my inner DataList to my Orders collection property?

atconway

I figured this out; just took a bit to get back in the ole' ASP.NET server controls mode ;)

The idea is to still use the ItemDataBound event, but inspect for both Item and AlternatingItem along with casting the DataItem to the CustomerObject. Then I proceeded to use the original collection bound to the DataList I had stored in session and use a quick Find() to get the proper object pulled back out. Once that's narrowed down, I can use it's .Orders collection to bind to the inner DataList.DataSource property. It works like a charm:

protected void ui_dlst_ETLMainInformation_ItemDataBound(object sender, DataListItemEventArgs e)
{

   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
      Customer c = e.Item.DataItem as Customer;
      DataList innerDataList = e.Item.FindControl("innerDataListControl") as DataList;

      List<Customers> customers = ((IList)Session["CustomersCollection"]).Cast<Customers>().ToList();

      Customer customer = customers.Find(ct => ct.ID == c.ID);

      innerDataList.DataSource = customer.Orders;
      innerDataList.DataBind();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Bind Odometer in Datalist

From Dev

How to bind collection dependency property in UserControl

From Dev

How to bind collection dependency property in UserControl

From Dev

How to bind property using a formatter

From Dev

How to bind the control in Datalist with new value?

From Dev

Using Datalist Items within a nested repeater

From Dev

How to bind ListBox ItemsSource to a ViewModel property (which is a Collection) programmatically?

From Dev

JavaFX bind choicebox to a property in a collection

From Dev

Does datalist tag need to be bind on serverside when not using database

From Dev

GroupBy with property in a nested child collection

From Dev

How to select nested property using Hibernate Criteria

From Dev

How to orderby in AngularJS using Nested property

From Dev

How to set nested property value using FastMember

From Dev

How to bind datalist from code behind in asp.net

From Dev

Bind RadiobuttonList inside DataList

From Dev

Bind odometer to Datalist or gidview

From Dev

How can I transform a collection into a Guava Multimap grouped by the elements of a nested collection property?

From Dev

Cannot bind an Observable Collection to an Attached Property on an UserControl

From Dev

Caliburn Micro - bind ListBox to property of objects in a collection

From Dev

How to bind textblock with property

From Dev

How to Bind Attached Property

From Dev

How to bind dynamic property?

From Dev

How to Bind the Property of an Instance?

From Dev

How to bind a ContentView to a property?

From Dev

How to get dynamic id using HiddenField in Datalist

From Dev

How to Bind an another Control on the Setter Value Property by using a Trigger?

From Dev

How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?

From Dev

bind to filtered object property knockout nested foreach

From Dev

JavaFX Bind UI to a nested class's property

Related Related

  1. 1

    How to Bind Odometer in Datalist

  2. 2

    How to bind collection dependency property in UserControl

  3. 3

    How to bind collection dependency property in UserControl

  4. 4

    How to bind property using a formatter

  5. 5

    How to bind the control in Datalist with new value?

  6. 6

    Using Datalist Items within a nested repeater

  7. 7

    How to bind ListBox ItemsSource to a ViewModel property (which is a Collection) programmatically?

  8. 8

    JavaFX bind choicebox to a property in a collection

  9. 9

    Does datalist tag need to be bind on serverside when not using database

  10. 10

    GroupBy with property in a nested child collection

  11. 11

    How to select nested property using Hibernate Criteria

  12. 12

    How to orderby in AngularJS using Nested property

  13. 13

    How to set nested property value using FastMember

  14. 14

    How to bind datalist from code behind in asp.net

  15. 15

    Bind RadiobuttonList inside DataList

  16. 16

    Bind odometer to Datalist or gidview

  17. 17

    How can I transform a collection into a Guava Multimap grouped by the elements of a nested collection property?

  18. 18

    Cannot bind an Observable Collection to an Attached Property on an UserControl

  19. 19

    Caliburn Micro - bind ListBox to property of objects in a collection

  20. 20

    How to bind textblock with property

  21. 21

    How to Bind Attached Property

  22. 22

    How to bind dynamic property?

  23. 23

    How to Bind the Property of an Instance?

  24. 24

    How to bind a ContentView to a property?

  25. 25

    How to get dynamic id using HiddenField in Datalist

  26. 26

    How to Bind an another Control on the Setter Value Property by using a Trigger?

  27. 27

    How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?

  28. 28

    bind to filtered object property knockout nested foreach

  29. 29

    JavaFX Bind UI to a nested class's property

HotTag

Archive