Re: adding user control programatically

Discussion in 'ASP .Net' started by Shailesh Ratadia, May 20, 2004.

  1. Hi Francois,

    Since a user control is dynamically generated, you cannot create the user
    control programatically using new. You might want to try the LoadContol
    method of the containing page which returns an object of type contol which
    you can cast to the strong type of your user control.

    Hope that helps.

    -Shailesh Ratadia

    "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 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
    >
    >
    >
     
    Shailesh Ratadia, May 20, 2004
    #1
    1. Advertisements

  2. Shailesh Ratadia

    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

    "Shailesh Ratadia" <> wrote in message
    news:...
    > Hi Francois,
    >
    > Since a user control is dynamically generated, you cannot create the user
    > control programatically using new. You might want to try the LoadContol
    > method of the containing page which returns an object of type contol which
    > you can cast to the strong type of your user control.
    >
    > Hope that helps.
    >
    > -Shailesh Ratadia
    >
    > "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 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
    #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. AC
    Replies:
    0
    Views:
    414
  2. francois

    adding user control programatically

    francois, May 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    476
    Scott Allen
    May 19, 2004
  3. Edgardo Rossetto
    Replies:
    2
    Views:
    449
    Craig Deelsnyder
    Apr 4, 2005
  4. Mad Scientist Jr
    Replies:
    0
    Views:
    892
    Mad Scientist Jr
    Mar 22, 2006
  5. Ben Dewey
    Replies:
    2
    Views:
    486
    Ben Dewey
    May 5, 2006
  6. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    805
    Jonathan Wood
    Feb 2, 2010
  7. Shawn

    Having trouble adding a web user control programatically.

    Shawn, Jul 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    296
    John Saunders
    Aug 1, 2004
  8. Travis Pruitt

    Dynamically Adding User Control with Child User Control

    Travis Pruitt, Jan 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    300
    Travis Pruitt
    Jan 13, 2005
Loading...