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. Advertising

  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. 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. Guest
    Replies:
    2
    Views:
    723
  2. 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:
    182
    Luis Ramírez
    Jul 6, 2004
  3. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

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

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    170
    Fabricio
    Sep 21, 2005
  5. Curt_C [MVP]

    Custom WebControl, custom property list

    Curt_C [MVP], Sep 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    163
    Curt_C [MVP]
    Sep 1, 2005
Loading...

Share This Page