Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Page.FindControl vs Parent.FindControl

Back to Forum: Tech Off
  • User profile image
    jsampsonPC

    Yesterday I spent a good while trying to get Page.FindControl(Control) to work for me so that I could run through a series of controls. It wouldn't work Smiley I was using the following code:

    Dim myVal As TextBox
    For i As Integer = 1 To 3
        myVal = Page.FindControl("txtReference" & i & "Name")
        Response.Write("<b>" & myVal.Text & "</b>")
    Next
    That wasn't working. So I tried this:
    Dim myVal As TextBox
    For i As Integer = 1 To 3
        myVal = txtReference1Name.Parent.FindControl("txtReference" & i & "Name")
        Response.Write("<b>" & myVal.Text & "</b>")
    Next
    And that worked...Why didn't the Control show up in the Page collection? I'm confused Smiley

  • User profile image
    jsampsonPC

    After checking it out a bit more, my control [txtReference1Name] is nested inside a WizardStep, which is inside a WizardControl, which is inside the Page.

    Perhaps that is why Page.FindControl didn't work, because txtReference1Name was nested inside a nested child of the Page Object.

    So when I ask for the Parent of a sibling control to the control I desire, I can get into the proper scope - makes sense now.

  • User profile image
    Adrian​JMartin

    From the Docs:

    The FindControl method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page.

    ms-help://MS.VSCC.v80/MS.MSDN.vAug06.en/cpref13/html/M_System_Web_UI_Page_FindControl_1_16219e3a.htm

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.