Referencing Repeater Data in a User Control from a Page

Discussion in 'ASP .Net Building Controls' started by BigJohn, Feb 7, 2007.

  1. BigJohn

    BigJohn Guest

    I have successfully displayed text boxes in a repeater. The repeater is in a
    User Control. The User Control is in a container which references a master
    page.

    I have another user control which contains text boxes. For this one, the
    simple syntax of:
    dim ctlWork as Textbox
    ctlWork = ctype(Me.ctnInformation.FindControl("txtFName"), Textbox)
    strWork = ctlWork.Text

    In the case where I am having problems, the objects are laid out as follows:

    Page Level:

    <asp:Content ID="Main" ContentPlaceHolderID="ContentPlaceHolder1"
    runat=server>
    <table width="100%" border="1" bordercolor=black >
    <tr>
    <td width="100%">
    <uc2:RegistrationE11 ID="RegistrationE11_1"
    runat="server" />
    </td>
    </tr>
    </table>
    </asp:Content>


    User Control:

    <Table ID="tblE1Data" Border=0 Width="100%" runat=server >
    <tr id="rowE1Update" runat=server>
    <td>
    <asp:TextBox ID="txtFName" runat=server MaxLength=100
    Width=140px></asp:TextBox> (textbox is filled in codebehind)
    <td>
    <asp:Repeater ID="repUpdate" runat=server>
    <HeaderTemplate >
    <table width=100% border=0>
    </HeaderTemplate>
    <ItemTemplate >
    <tr>
    <td>
    <asp:textbox ID="RegIDE1" runat=server
    Text=<%# Databinder.Eval(container.dataitem, "RegID") %>></asp:TextBox>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </td>
    </tr>
    </Table>


    The intention is to have the user udpate the text boxes (there is more to
    this project than what is shown of course), click the Update button on the
    page, and apply the changes.

    I tried Me.RegistrationE11_1.FindControl("RegIDE1") but the resulting type
    is Nothing.

    What is the syntax to look at all the textbox "RegIDE1" values from the
    repeater on the user control in the main page?
     
    BigJohn, Feb 7, 2007
    #1
    1. Advertisements

  2. Hello John,

    From your description, you're encountering some problem when try to access
    a TextBox in the repeater control's template and the repeater is inside a
    usercontrol which are used in the main page, correct?

    Based on the code your provided, I think the problem here is that you
    directly try locating the "Textbox"(which is inside repeater's
    itemTemplate) through UserControl.FindControl method. This won't work since
    you need to first get reference to the repeater control, and then locate
    the certain RepeaterItem (since it will contains multiple RepeaterItem
    after performing databinding). After that, you can call "FindControl"
    method on the RepeaterItem instance to locate the TextBox. So the code
    will look like below:

    ===================
    Dim rpt As Repeater
    rpt = RegistrationE11 .FindControl("repUpdate")

    If Not rpt Is Nothing Then

    For Each item As RepeaterItem In rpt.Items

    Dim txt As TextBox = item.FindControl("RegIDE1")

    txt.Text = txt.Text & "_updated"

    Next

    End If

    End Sub
    ====================

    Anyway, the basic rule is that if the control you want to locate is nested
    in another control(which has implemented INamingContainer interface), you
    need to find that control first and then call FindControl recursively to
    locate its child controls.

    BTW, for inspecting the control hierarchy in ASP.NET page, you can turn on
    the output trace in the @Page directive. This is very help since it will
    display the complete ASP.NET server control tree of the current page. You
    can get the path you need to go through when try locating a control:

    <%@ Page Language="C#" ............................. Trace="true"%>

    Hope this helps.


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Feb 8, 2007
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.