views:

364

answers:

4

Hi Everyone,

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

A: 

OK; I've got a solution in place, but I'll leave this open for a bit in case anybody can come up with a better one.

Basically, I'm taking the MinPrice and MaxPrice values and dumping them into a HiddenField outside of the nested ListView.

Then, inside the nested one, I'm drilling up (Container.Parent), finding the HiddenField, and then extracting its value.

Jim B
A: 

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.

Slauma
Very good point, but the actual DataModel is a bit convoluted under the covers. What I'm actually binding to is a custom object that is created strictly for databinding (i.e. i take a whole bunch of other info, and then assemble it all together into the object that is being bound). Unfortunately, I don't have a way to go back upstream on this one.
Jim B
A: 

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

Luis