ASP.Net Edit Child Collection with DataList

ASP.Net Edit Child Collection with DataList

Old forum URL: forums.lhotka.net/forums/t/8305.aspx


dmbRedGetta posted on Thursday, January 07, 2010

I'm trying to edit a child collection using a DataList (so that the child collection is saved when the parent is saved), but I'm not sure how to get the information out of the DataList and back into my child collection.

I'm using a CSLADataSource to bind the List, and during the DataSource_SelectObject event I'm linking the BusinessObject up to the child collection object.  That works and the list is rendered with the rest of the information on the page.

I'm probably over-thinking it, but I can't figure out how to get the information out of the DataList to push into the child collection.  The UpdateObject event on that DataSource is never called since I'm not updating one row at a time, so do I need to loop the DataList rows and copy the information from there back into the child collection during the root object's Update event?

On a Windows Forms app I did a while back, it was simply a matter of setting the BindingSource to link up with the appropriate DataMember of the root object.

dmbRedGetta replied on Thursday, January 07, 2010

Sorry, I guess I've been a little too deep in this...

I tried it out, and looping the DataList items and pulling the values out of the DataList controls and pushing those values back into my child list worked perfectly.

I was concered about how to identify a new list item (that didn't yet have an ID value since that is assigned in the database), but keeping the same order and using the DataListItem's ItemIndex property to identify the index of the item in the child collection worked just fine.

fark replied on Saturday, March 06, 2010

Would you mind posting a sample of how you did this.  I've been trying to do something similar and need some guidance.

dmbRedGetta replied on Tuesday, March 16, 2010

Add an additional CSLA Data Source to the page that will bind your child collection, and code that data source's SelectObject method to link up with the child collection:

e.BusinessObject = DirectCast(GetOrderForm(), OrderForm).Fields

Then, during your main data source's insert and update events, call a method that loops through the DataList and rebuilds the child collection before saving the main object:

Private Sub UpdateFields()
     Dim obj As OrderForm = GetOrderForm()
    
For Each item As DataListItem In FieldsDataList.Items
          obj.Fields(item.ItemIndex).Label =
CType(item.FindControl("FieldLabelTextBox"), TextBox).Text
          obj.Fields(item.ItemIndex).Type =
CType(item.FindControl("FieldTypeDropDownList"), DropDownList).SelectedValue
    
Next
End Sub

Protected Sub OrderFormDataSource_UpdateObject(ByVal sender As Object, ByVal e As Csla.Web.UpdateObjectArgs) Handles OrderFormDataSource.UpdateObject
     Dim obj As OrderForm = GetOrderForm()
     Csla.Data.DataMapper.Map(e.Values, obj,
"ID")
     UpdateFields()
     SaveOrderForm(obj)
End Sub Hope this helps!

Copyright (c) Marimer LLC