Board index » Web Programming » Tough Question: Accessing controls inside inline template in a user control, in a repeater

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

Web Programming241
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!


-
 

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

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)





"Dave" <nospam@spam3mail.com>wrote in message

Quote
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!







-