Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Loading User Controls at Runtime

Loading User Controls at Runtime

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpwinformsdebuggingsales
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joffers
    wrote on last edited by
    #1

    I am currently creating a Web application that uses a master page, a .aspx page and then a couple of user controls. Depending on the choice the user selects, I load the selected control into a placeholder. Everything works when I debug but when the application goes live I get the following error at runtime when trying to load the controls: System.InvalidCastException: Unable to cast object of type 'ASP.customer_details_ascx' to type 'Details'. This is what the code looks like, and the error is thrown on this line: Details ctrl1 = (Details)LoadControl( "~/Customer/Details.ascx" ); protected void Details_Click() { Details ctrl1 = (Details)LoadControl( "~/Customer/Details.ascx" ); PlaceHolder1.Controls.Clear(); PlaceHolder1.Controls.Add( ctrl1 ); } I have changed the "batch=false" in the web.config, and have changed all the <%@ Reference %> directives to <%@ Register %> directives in the Main .aspx page. But none of this seems to fix the problem. Please help with any suggestions, I have searched the web for hopeful answers to the problem but they did not work (they were the <%@ Register %> and "batch=false"). It seems to be a problem that many people are facing. Thanks Jeff

    P 1 Reply Last reply
    0
    • J Joffers

      I am currently creating a Web application that uses a master page, a .aspx page and then a couple of user controls. Depending on the choice the user selects, I load the selected control into a placeholder. Everything works when I debug but when the application goes live I get the following error at runtime when trying to load the controls: System.InvalidCastException: Unable to cast object of type 'ASP.customer_details_ascx' to type 'Details'. This is what the code looks like, and the error is thrown on this line: Details ctrl1 = (Details)LoadControl( "~/Customer/Details.ascx" ); protected void Details_Click() { Details ctrl1 = (Details)LoadControl( "~/Customer/Details.ascx" ); PlaceHolder1.Controls.Clear(); PlaceHolder1.Controls.Add( ctrl1 ); } I have changed the "batch=false" in the web.config, and have changed all the <%@ Reference %> directives to <%@ Register %> directives in the Main .aspx page. But none of this seems to fix the problem. Please help with any suggestions, I have searched the web for hopeful answers to the problem but they did not work (they were the <%@ Register %> and "batch=false"). It seems to be a problem that many people are facing. Thanks Jeff

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You don't actually need to convert the control. All you need to do is replace this with:

      Control ctrl = LoadControl("~/Customer/Details.ascx");
      

      Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups