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

Discussion in 'ASP .Net' started by Dave, Dec 20, 2006.

  1. Dave

    Dave Guest

    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!
     
    Dave, Dec 20, 2006
    #1
    1. Advertising

  2. Dave

    Dave Guest

    FIXED: 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" <> wrote in message
    news:flaih.142734$...
    >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!
    >
     
    Dave, Dec 22, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fernandezr
    Replies:
    8
    Views:
    445
    fernandezr
    Jul 7, 2006
  2. HockeyFan
    Replies:
    1
    Views:
    1,013
    Masudur
    Jul 4, 2007
  3. Justin Dutoit
    Replies:
    0
    Views:
    357
    Justin Dutoit
    Mar 19, 2008
  4. Ashish Bandi via .NET 247

    Accessing TextBox/Label controls inside a Repeater control

    Ashish Bandi via .NET 247, May 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    811
    Scott Mitchell [MVP]
    May 14, 2004
  5. Kiran More
    Replies:
    2
    Views:
    328
    Kiran More
    Nov 14, 2006
Loading...

Share This Page