Domanda

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?

È stato utile?

Soluzione

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();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top