Controls.Add() for a Web User Control doesn't instansiate its child controls

Discussion in 'ASP .Net' started by Ofer Zelig, Mar 26, 2007.

  1. Ofer Zelig

    Ofer Zelig Guest

    I'll describe the simplest situation of the problem.

    I have a simple Web User Control which only contains a: <div id="bla"
    runat="server" /> .
    I dynamically add it to a page, by performing:

    SomeCtl ctl = new SomeCtl();
    this.Controls.Add(ctl);

    and of course apply a <% @ Register %> tag in order to make SomeCtl known to
    the page (is there some more elegant way?).

    I override OnPreRender of the control, in order to write some stuff to the
    DIV according to some property of the control, but I can't (within the
    event code) access the DIV. It is equal to null.
    The issue happens of course only when my user control (SomeCtl) is added
    dynamically to the page, and not when I place the user control directly
    within the page at design time.


    Thanks in advance for your help,
    Ofer
     
    Ofer Zelig, Mar 26, 2007
    #1
    1. Advertising

  2. Hi Ofer,

    It seems you might not have seen my reply in your previous post so I'm
    replying here again for your reference:

    I'm not sure if I've fully understood your question, so please feel free to
    correct me:

    1) You have a web user control which need to load another copy of itself
    into its control hierarchy. I think you must have some logic to prevent the
    control from recursively loading infinitely, would you please show the
    logic here?

    2) Normally we use LoadControl to dynamically load a web user control
    instead of using "new":

    #Overview of user controls vs. custom controls
    http://support.microsoft.com/kb/893667


    Would you please elaborate more on your control so that I can give more
    detailed and specific suggestion? Thanks.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Mar 26, 2007
    #2
    1. Advertising

  3. RE: Controls.Add() for a Web User Control doesn't instansiate its chil

    Good morning,

    Web user controls have to be instantiated by calling LoadControl() method of
    the page. Replace
    SomeCtl ctl = new SomeCtl();
    this.Controls.Add(ctl);

    with:

    // make sure path is correct
    SomeCtl ctl = (SomeCtl) LoadControl("SomeControl.ascx");
    this.Controls.Add(ctl);

    Then, calling FindControl() will return valid HtmlGenericControl instance
    representing your div. In addition, if you load controls programatically, you
    don't need to use @Register directive on the page. It's only necessary if you
    use put web user control in aspx code.

    hope this helps

    --
    Milosz


    "Ofer Zelig" wrote:

    > I'll describe the simplest situation of the problem.
    >
    > I have a simple Web User Control which only contains a: <div id="bla"
    > runat="server" /> .
    > I dynamically add it to a page, by performing:
    >
    > SomeCtl ctl = new SomeCtl();
    > this.Controls.Add(ctl);
    >
    > and of course apply a <% @ Register %> tag in order to make SomeCtl known to
    > the page (is there some more elegant way?).
    >
    > I override OnPreRender of the control, in order to write some stuff to the
    > DIV according to some property of the control, but I can't (within the
    > event code) access the DIV. It is equal to null.
    > The issue happens of course only when my user control (SomeCtl) is added
    > dynamically to the page, and not when I place the user control directly
    > within the page at design time.
    >
    >
    > Thanks in advance for your help,
    > Ofer
    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 28, 2007
    #3
  4. Hi Ofer,

    As other community members replied, using LoadControl should be the key to
    your issue here. Would you please let us know if this suggestion works for
    you? Please feel free to let us know if there's anything unclear. Thanks.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Mar 28, 2007
    #4
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,200
    David Schwartz
    Dec 9, 2003
  2. Ofer Zelig
    Replies:
    15
    Views:
    916
    Mark Rae
    Mar 28, 2007
  3. Chad Scharf
    Replies:
    3
    Views:
    944
    Chad Scharf
    Oct 18, 2007
  4. Replies:
    0
    Views:
    208
  5. thunk
    Replies:
    1
    Views:
    359
    thunk
    Mar 30, 2010
Loading...

Share This Page