Access Parent DataItem in Nested ListView
So I've got two listviews; one nested inside the other开发者_如何学JAVA.
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
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
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.
Here's another hack: http://bytes.com/topic/asp-net/answers/536803-finding-parent-control-value-nested-datalist-c
If you have been set DataKeyNames in the parent, then you can access it like this
((ListView)Container.Parent.Parent.Parent.Parent).DataKeys[((ListViewDataItem)Container.Parent.Parent.Parent).DataItemIndex][0]
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.
精彩评论