Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Tough Question: Accessing controls inside inline template in a user control, in a repeater

Reply
Thread Tools

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

 
 
Dave
Guest
Posts: n/a
 
      12-20-2006
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!


 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      12-22-2006
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" <(E-Mail Removed)> wrote in message
news:flaih.142734$(E-Mail Removed) o.uk...
>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!
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tough Q on Dynamically adding cached User Controls Justin Dutoit ASP .Net 0 03-19-2008 12:46 AM
RowCommand: accessing ObjectDataSource from inside gridview inside a repeater? HockeyFan ASP .Net 1 07-04-2007 05:49 AM
Accessing Server Controls inside a Repeater? John Kotuby ASP .Net 4 02-27-2007 09:12 PM
Accessing USER CONTROL which is inside Masterpagethrough Another USER Control inside normal page. Kiran More ASP .Net Web Controls 2 11-14-2006 12:58 PM
Accessing TextBox/Label controls inside a Repeater control Ashish Bandi via .NET 247 ASP .Net Web Controls 1 05-14-2004 07:10 PM



Advertisments