how to add a user control progrmatically?

Discussion in 'ASP .Net' started by francois, May 19, 2004.

  1. francois

    francois Guest

    Hi,

    I would like to add a USER control (not custom control) programmatically to
    my aspx page.
    I am using something like the following in my page_load code :

    *
    Bos.UserControls.HeaderFinance header = new HeaderFinance();
    this.Controls.Add(header);
    *

    The problem is that asp.net controls existing in my user control are not
    instanciated and are still 'null', then of course it does not work.

    Then is there a way to add a user control to a page programatically? Or at
    least dynamically. I want to add different user controls on my page
    depending of some runtime parameters.

    Best regards,

    Francois
    francois, May 19, 2004
    #1
    1. Advertising

  2. Use the Page.LoadControl() method to load the User Control at run-time. It
    returns a reference to the Control you just loaded:

    Dim c As MyUserControl = CType(Page.LoadControl("MyUserControl.ascx"),
    MyUserControl)

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "francois" <_NOSPAM> wrote in message
    news:...
    > Hi,
    >
    > I would like to add a USER control (not custom control) programmatically

    to
    > my aspx page.
    > I am using something like the following in my page_load code :
    >
    > *
    > Bos.UserControls.HeaderFinance header = new HeaderFinance();
    > this.Controls.Add(header);
    > *
    >
    > The problem is that asp.net controls existing in my user control are not
    > instanciated and are still 'null', then of course it does not work.
    >
    > Then is there a way to add a user control to a page programatically? Or

    at
    > least dynamically. I want to add different user controls on my page
    > depending of some runtime parameters.
    >
    > Best regards,
    >
    > Francois
    >
    >
    >
    Kevin Spencer, May 19, 2004
    #2
    1. Advertising

  3. francois

    francois Guest

    I have done it but now I have the following error :

    The Controls collection cannot be modified because the control contains code
    blocks (i.e. <% ... %>).

    Does that mean that the user controls I add programatically are forbidden to
    haveany <% %> tags? Or did I left out something?

    Francois


    "Kevin Spencer" <> wrote in message
    news:...
    > Use the Page.LoadControl() method to load the User Control at run-time. It
    > returns a reference to the Control you just loaded:
    >
    > Dim c As MyUserControl = CType(Page.LoadControl("MyUserControl.ascx"),
    > MyUserControl)
    >
    > --
    > HTH,
    > Kevin Spencer
    > .Net Developer
    > Microsoft MVP
    > Big things are made up
    > of lots of little things.
    >
    > "francois" <_NOSPAM> wrote in message
    > news:...
    > > Hi,
    > >
    > > I would like to add a USER control (not custom control) programmatically

    > to
    > > my aspx page.
    > > I am using something like the following in my page_load code :
    > >
    > > *
    > > Bos.UserControls.HeaderFinance header = new HeaderFinance();
    > > this.Controls.Add(header);
    > > *
    > >
    > > The problem is that asp.net controls existing in my user control are not
    > > instanciated and are still 'null', then of course it does not work.
    > >
    > > Then is there a way to add a user control to a page programatically? Or

    > at
    > > least dynamically. I want to add different user controls on my page
    > > depending of some runtime parameters.
    > >
    > > Best regards,
    > >
    > > Francois
    > >
    > >
    > >

    >
    >
    francois, May 20, 2004
    #3
    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. Dan
    Replies:
    1
    Views:
    5,384
    Christophe Gijbels
    Jan 4, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    626
    Mad Scientist Jr
    Mar 22, 2006
  3. Chad Scharf
    Replies:
    3
    Views:
    860
    Chad Scharf
    Oct 18, 2007
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    530
    Jonathan Wood
    Feb 2, 2010
  5. David C

    progrmatically created template columns upon postback

    David C, Dec 21, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    97
    David C
    Dec 21, 2005
Loading...

Share This Page