Loading User Controls at Runtime
-
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
-
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
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.