Tough Question: Accessing controls inside inline template in a user control, in a repeater

D

Dave

I have the following ASP.NET 2.0 code (simplified here for ease):

<asp:Repeater id="SearchResultsRepeater" runat="server">
<ItemTemplate>
<uc:SearchResult ID="SearchResult"
ResultObject="<%#Container.DataItem%>" runat="server">
<ButtonsTemplate>
<uc:ViewButton ID="ViewButton"
ListingReference='<%#Eval("ListingReference")%>' runat="server" />
</ButtonsTemplate>
</uc:SearchResult>
</ItemTemplate>
</asp:Repeater>

This works fine except for the databinding on user control "ViewButton"
property ListingReference. Does anyone know how to get the databinding to
work in this example?

For reference moving <uc:ViewButton> outside the inline template will work
with no problem, but is not what I want:

<asp:Repeater id="SearchResultsRepeater" runat="server">
<ItemTemplate>
<uc:SearchResult ID="SearchResult"
ResultObject="<%#Container.DataItem%>" runat="server" />
<ButtonsTemplate>

</ButtonsTemplate>
</uc:SearchResult>
<uc:ViewButton ID="ViewButton"
ListingReference='<%#Eval("ListingReference")%>' runat="server" />
</ItemTemplate>
</asp:Repeater>

I have also tried accessing <uc:ViewButton> programmatically but I don't
know where to find the control:

Protected Sub SearchResultsRepeater_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
SearchResultsRepeater.ItemDataBound
Dim result As SearchResultObject = CType(e.Item.DataItem,
SearchResultObject)
If result IsNot Nothing Then
Dim resultUserControl As SearchResult =
CType(e.Item.FindControl("SearchResult"), SearchResult)
' FOR EXAMPLE THIS WILL NOT WORK:
' Dim viewButton As ViewButton =
CType(resultUserControl.FindControl("ViewButton"), ViewButton)
' viewButton.ListingReference = result.ListingReference
End If
End Sub

Does anyone know how I find the control in this example?

Thanks!
 
D

Dave

After using a hack that used CSS to place the buttons using positioning, I
decided to revisit this and come up with a proper ASP.NET solution. My user
control "Search Result" contains the following code:

Protected _buttonsTemplate As ITemplate = Nothing

<PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(GetType(TemplateControl))> _
Public Property ButtonsTemplate() As ITemplate
Get
Return _buttonsTemplate
End Get
Set(ByVal value As ITemplate)
_buttonsTemplate = value
End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If _buttonsTemplate IsNot Nothing Then
_buttonsTemplate.InstantiateIn(ButtonsPlaceHolder)
End If
End Sub

I added an overrided CreateChildControls() routine and moved the code from
the Page_Load event:

Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
If _buttonsTemplate IsNot Nothing Then
_buttonsTemplate.InstantiateIn(ButtonsPlaceHolder)
End If
End Sub

I can now use FindControl to access any controls I place in the
<ButtonsTemplate> inline template (see below)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top