Hope someone will serve somewhere.
Here is my code to make this work:
1) I have a Listview that has a custom control when editing. This custom cotnrol itself has a list inside
<asp:ListView runat=server ID=C_LV_MyObjects DataKeyNames="Id" OnItemDataBound=DataBoundMyObjects OnItemEditing=ItemEditing > <LayoutTemplate> <table runat=server id="itemPlaceholderContainer"> <tr> <th> Description </th> </tr> <tr runat="server" id="itemPlaceholder"> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td> text... </td> <td> <asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton> </td> <td> <asp:LinkButton runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton> </td> </ItemTemplate> <EditItemTemplate> <tr> <td colspan=3> <MyTag:MyUC ID=C_UC_MyUserControl runat=server OnEditing=MyObjectEditing /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> No results found! </EmptyDataTemplate> </asp:ListView>
The C # code for this list is as follows:
public int EditIndexComposition; protected void ItemEditing(object sender, ListViewEditEventArgs e) { C_LV_MyObjects.EditIndex = e.NewEditIndex; C_LV_MyObjects.DataBind(); } protected void MyObjectEditing(object sender, EventArgs e) { ListViewEditEventArgs MyEvent = (ListViewEditEventArgs)e; if (MyEvent != null) EditIndexComposition= MyEvent.NewEditIndex; C_LV_MyObjects.DataBind(); } protected void DataBoundMyObjects(object sender, ListViewItemEventArgs e) { MyUC uc = (MyUC)e.Item.FindControl("C_UC_MyUserControl"); if (uc!=null) { uc.EditIndex = EditIndexComposition; ListViewDataItem dataItem = (ListViewDataItem)e.Item; MyObject obj= (MyObject)dataItem.DataItem; uc.DataSource=Myservice.GetDatasource(obj.Id); uc.DataBind(); } }
and the code of my Usercontrol is as follows:
<asp:PlaceHolder runat="server" ID="C_PH_ObjComposition"> <asp:ListView runat="server" ID="C_LV_AppaltatoreComposizione" DataSource="<% # DataSource %>" DataKeyNames="Id" OnItemEditing="ItemEditing"> etc... <ItemTemplate> <tr> <td> <asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton> </td> </tr> </ItemTemplate> <EditItemTemplate> <tr> <td> Edit Mode </td> </tr> </EditItemTemplate> </asp:ListView> </asp:PlaceHolder>
with the following C # code:
public int EditIndex { get {return C_LV_ObjComposition.EditIndex;} set { C_LV_ObjComposition.EditIndex=value;} } public event EventHandler Editing; protected void ItemEditing(object sender, ListViewEditEventArgs e) { C_LV_ObjComposition.EditIndex = e.NewEditIndex; if (Editing != null) Editing(this, e); }
When you click the edit button of the internal list, we save the index that was clicked, and we run the function in the first user control for the container. This function will be stored in a global value that is indexed by the client and triggers data binding in the outter list. Thus, we get onitemdatabound, which recreates our user control with the corresponding values, we can then assign the index of the edit line to the data binding usercontrol.
That is all, if you have any questions, please feel free to answer.
ciao!