Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Parent DataItem in Nested ListView

So I've got two listviews; one nested inside the other.

The parent is being bound to a collection of objects that contain fields such as MaxPrice, MinPrice, and SuggestedProducts.

The nested one is being bound to the SuggestedProducts collection of the parent item.

How could I reference MaxPrice and MinPrice in the nested listview? Is it even possible?

If you need any clarification, leave me a comment and I'll update my question.

Thanks!

Edit: This is an ASP.NET ListView

like image 886
Jim B Avatar asked Jul 03 '26 06:07

Jim B


2 Answers

I just had the same issue and I found another solution that I would like to share.

From the ItemDataBound event on the child nested ListView you can get the reference from the parent doing something like this:

ListViewDataItem CurrentParentItem = (ListViewDataItem)e.Item.Parent.Parent.Parent;
ParentObject parentObject = CurrentParentItem.DataItem as ParentObject
//Then you can access to parentObject.MaxPrice & parentObject.MinPrice

Hope this help people with the same problem

like image 50
Luis Avatar answered Jul 05 '26 21:07

Luis


If your SuggestedProduct class would have a reference back to its parent class X (so you'd have a bidrectional data model: X has a collection of SuggestedProducts and SuggestedProduct has an object reference to X) you could give SuggestedProduct properties like MinPrice { get {return parentX.MinPrice;} } (and perhaps also set) and then use Eval("MinPrice") (and perhaps also Bind) in your nested ListView.

Just as an idea in case that modification of your class model is a real and easy option.

like image 36
Slauma Avatar answered Jul 05 '26 19:07

Slauma