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

  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. 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. AC
    Replies:
    0
    Views:
    338
  2. francois

    adding user control programatically

    francois, May 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Scott Allen
    May 19, 2004
  3. Ben Dewey
    Replies:
    2
    Views:
    440
    Ben Dewey
    May 5, 2006
  4. Shawn

    Having trouble adding a web user control programatically.

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

    Dynamically Adding User Control with Child User Control

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

Share This Page