How to Dynamically Load User Controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abehm
    New Member
    • May 2007
    • 35

    How to Dynamically Load User Controls

    I have a couple placeholders in which I want to dynamically load user controls into a multi-pageview based on db data.

    The database returns the filename of the user control (i.e. UserControl.asc x), and a value (tdr.Page) to determine which placeholder it should load into.
    I then use Server.MapPath to get the path to the user control but when it comes down to Load the control I keep getting the error "filepath\userc ontrol.ascx is not a valid virtual path". Can anyone please help me solve this problem?

    here is the code i am using:
    [CODE="c"]
    string component = tdr.UserControl ;
    string componentPath = Server.MapPath( component);

    switch (tdr.Page)
    {
    case "Page1":
    {
    PlaceHolder ph = (PlaceHolder)Pa geView1.FindCon trol("Page1Plac eHolder");
    ph.Controls.Add (Page.LoadContr ol(componentPat h)); //where I get the error
    break;
    }
    case "Page2":
    {
    PlaceHolder ph = (PlaceHolder)Pa geView2.FindCon trol("Page2Plac eHolder");
    ph.Controls.Add (Page.LoadContr ol(componentPat h)); //error
    break;
    }
    }

    [/CODE]
Working...