Template.LoadControl(type,object[])...doesn't work as expected

Discussion in 'ASP .Net' started by Keith Patrick, Nov 15, 2005.

  1. Maybe my expectations are just too low because I haven't read much
    documentation on this method, but basically, I am trying to load a user
    control dynamically without using a virtual path (I have disabled
    FileIOPermission beyond AllLocalFiles, and it appears that
    LoadControl(string) calls Server.MapPath, which needs greater access). What
    I am attempting to do is just pass in my UC type:
    Control ctrl = this.Page.LoadControl(TestApp.Controls.MyUserControl, null);

    but when I do this, the control doesn't show up. I've looked at the
    differences between controls loaded this way and via virtual path, as the
    virtual path version displays just fine. The underlying types, however are
    different. The virtualpath one is of type "ASP.MyUserControl_aspx" whereas
    the one loaded from type is TestApp.Controls.MyUserControl and also lacks
    TemplateXXXX properties. Am I somehow misuing LoadControl(type, object[])
    or is there a bug that prevents dynamic control loading like this?
     
    Keith Patrick, Nov 15, 2005
    #1
    1. Advertising

  2. Keith Patrick

    Bruce Barker Guest

    you are loading the codebehind class, so if the code behind depends on
    anything in the aspx file, it won't work. the loadcontrol from virtual path
    will compile the aspx page if needed, and uses its class, not the codebehind
    class.

    you should switch to server (code only) controls if you want to use this
    technique.

    -- bruce (sqlwork.com)


    "Keith Patrick" <> wrote in message
    news:%...
    > Maybe my expectations are just too low because I haven't read much
    > documentation on this method, but basically, I am trying to load a user
    > control dynamically without using a virtual path (I have disabled
    > FileIOPermission beyond AllLocalFiles, and it appears that
    > LoadControl(string) calls Server.MapPath, which needs greater access).
    > What I am attempting to do is just pass in my UC type:
    > Control ctrl = this.Page.LoadControl(TestApp.Controls.MyUserControl,
    > null);
    >
    > but when I do this, the control doesn't show up. I've looked at the
    > differences between controls loaded this way and via virtual path, as the
    > virtual path version displays just fine. The underlying types, however
    > are different. The virtualpath one is of type "ASP.MyUserControl_aspx"
    > whereas the one loaded from type is TestApp.Controls.MyUserControl and
    > also lacks TemplateXXXX properties. Am I somehow misuing
    > LoadControl(type, object[]) or is there a bug that prevents dynamic
    > control loading like this?
    >
     
    Bruce Barker, Nov 15, 2005
    #2
    1. Advertising

  3. I knew it worked that way in 1.1, but I would think that since 2.0
    implements them as partial classes, they would be linked together at compile
    time such that my namespace-qualified classname also corresponds to the
    full, non-partial class, since I can go into TestApp.Controls.MyUserControl
    and reference controls that have only been declared in the codebehind at
    design-time.
     
    Keith Patrick, Nov 16, 2005
    #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. DC
    Replies:
    0
    Views:
    730
  2. Alex Buell
    Replies:
    6
    Views:
    324
    Alex Buell
    Feb 23, 2006
  3. Replies:
    1
    Views:
    2,150
    Gianni Mariani
    Jun 8, 2007
  4. thomas
    Replies:
    23
    Views:
    1,956
    Andrey Tarasevich
    Feb 22, 2008
  5. Angus
    Replies:
    8
    Views:
    322
    James Kanze
    Jun 18, 2009
Loading...

Share This Page