dynamically add usercontrol

Discussion in 'ASP .Net Building Controls' started by Snorre, Jun 14, 2005.

  1. Snorre

    Snorre Guest

    Hi
    Can anybody explain to me how to dynamically add a usercontrol? When I try
    to use the this.Controls.Add the control is added, but none of the
    usercontrol member controls are instanciated.

    The workaround I have done is to add the control to the aspx and set visible
    to false. Not very elegant, so I would really like to add the control from
    the codebehind file instead.
    Snorre, Jun 14, 2005
    #1
    1. Advertising

  2. Snorre

    Matt Guest

    Are you loading it before you add it?
    Matt, Jun 14, 2005
    #2
    1. Advertising

  3. Snorre

    Snorre Guest

    I'm doing now :) thanks.

    I find it strange though that I have to refer to the ascx file to make the
    page aware of the class (through LoadControl). Do you know of a more
    sophisticated way to do this?



    "Matt" wrote:

    > Are you loading it before you add it?
    >
    >
    Snorre, Jun 14, 2005
    #3
  4. Snorre

    Matt Guest

    The ascx file is parsed at runtime ... I think it has weird
    auto-generated class names like aspx files do ... you could write
    custom controls, but then you have to write your own designer classes
    .... In my situation, I have a default page with a place holder into
    which I dynamically load user controls ... I have a sitemap.xml
    document with entries for each user control containing the path, title,
    name etc. ... when I want to load a user control I look up the path in
    the xml document ... that might give you a little more separation
    between your code and physical path to the user controls ... if that's
    what you're looking for
    Matt, Jun 14, 2005
    #4
  5. Snorre

    Ron Guest

    Hi,
    I am trying to accomplish this same task would i really appreciate some code
    examples if possible.


    "Matt" wrote:

    > The ascx file is parsed at runtime ... I think it has weird
    > auto-generated class names like aspx files do ... you could write
    > custom controls, but then you have to write your own designer classes
    > .... In my situation, I have a default page with a place holder into
    > which I dynamically load user controls ... I have a sitemap.xml
    > document with entries for each user control containing the path, title,
    > name etc. ... when I want to load a user control I look up the path in
    > the xml document ... that might give you a little more separation
    > between your code and physical path to the user controls ... if that's
    > what you're looking for
    >
    >
    Ron, Feb 24, 2006
    #5
    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. huobazi
    Replies:
    1
    Views:
    662
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    392
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,519
  4. Phl
    Replies:
    2
    Views:
    504
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  5. Paul
    Replies:
    3
    Views:
    3,714
    Ben Amada
    Feb 25, 2009
Loading...

Share This Page