Niluse
Niluse

Reputation: 31

Referencing DataItem of parent Repeater control inside a User Control

I need to place a user control into the template of a repeater control and reference data items from the repeater's datasource.

I tried (ignore the second cast which is specific to the CMS platform I'm using):

<%# ((EPiServer.Core.PageData)((RepeaterItem)Container.Parent.NamingContainer).DataItem)["PageName"]%>

But that throws back the following error: Unable to cast object of type 'ASP.usercontrols_searchcontrols_searchresult_ascx' to type 'System.Web.UI.WebControls.RepeaterItem'

Searchresult_ascx is another user control that contains the actual repeater.

Upvotes: 3

Views: 3994

Answers (3)

Greg B
Greg B

Reputation: 14888

I would add an OnItemDataBound event to the repeater and from there bind the appropriate data to the user control.

Upvotes: 0

Allan Thraen
Allan Thraen

Reputation: 151

I would add a property on the usercontrol to hold the container - like this:

<asp:repeater ... >
  <my:usercontrol containerdata='<%# Container.DataItem %>' ... />
</asp:repeater>

And of course inside the user control databind to the PageData item you are passing along.

Upvotes: 5

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

It sounds like you have more parents in the hierarchy to get to the control desired. I often use the immediate window in debug mode to find the depth I have to back out OR use the Trace="True" on the web page and look at the control tree to see the hierarchy. From that you should be able to figure out your code to get at the proper parent control.

Upvotes: 0

Related Questions