Access WebControl in custom control

Discussion in 'ASP .Net Building Controls' started by William Leary, Oct 8, 2003.

  1. I cannot access a server control that is nested in a
    template control...like so:

    <dtag:pageTemplate runat="server" id="myPageTemplate">
    <MAINCONTENT><asp:placeHolder ID="quoteContent"
    runat="server"></asp:placeHolder>
    </MAINCONTENT>
    </dtag:pageTemplate>

    The contents of the MAINCONTENT tag are placed into the
    page via the following in the PageTemplate class:

    _mainContentContainer = new TemplateItem(this);
    MainContent.InstantiateIn(_mainContentContainer);
    _mainTableRow5Cell3.Controls.Add(_mainContentContainer);

    Everything works great, except for when I'm using
    WebControls and trying to access them in the code-behind
    page. I cannot access them. I have a variable declared of
    type PlaceHolder, but it is always null when I access it.
    FindControl using "quoteContent" doesn't work from the
    page, of from the myPageTemplate object.

    I suspect this has something to do with naming
    containers, but very mystified as to how to go about this
    at all. Have searched online for several hours now, to
    now avail. Any thoughts would be appreciated. Thanks.
     
    William Leary, Oct 8, 2003
    #1
    1. Advertisements

  2. Found the answer! It's necessary to recrusively search
    the controls on the page, as the control I made was
    several levels down in a control collection. The
    following function works:

    public static object RecursiveFindControl(Control
    control, string id)
    {
    object o = control.FindControl(id);

    if (o != null) { return o; }
    else
    {
    foreach (Control c in control.Controls)
    {
    o = RecursiveFindControl(c, id);

    if (o != null) { return o; }
    }

    return null;
    }
    }
     
    William Leary, Oct 8, 2003
    #2
    1. Advertisements

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. Guest
    Replies:
    2
    Views:
    919
  2. Matt Sokol
    Replies:
    2
    Views:
    629
    Theo Tillotson
    Aug 7, 2003
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    417
    Luis Ramírez
    Jul 6, 2004
  4. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    327
    Kasabaarde Sumta
    Dec 13, 2004
  5. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    339
    Fabricio
    Sep 21, 2005
  6. Peter Morris [Air Software Ltd]

    WebControl with non-WebControl property

    Peter Morris [Air Software Ltd], Dec 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    239
    Peter Morris [Air Software Ltd]
    Dec 7, 2004
  7. Curt_C [MVP]

    Custom WebControl, custom property list

    Curt_C [MVP], Sep 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    304
    Curt_C [MVP]
    Sep 1, 2005
  8. Steven

    Adding a WebControl to a WebControl

    Steven, Oct 13, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    203
    Steven
    Oct 13, 2008
Loading...