How to dynamic load ASP.NET User Controls in Visual Studio 2005

Did anybody know how to dynamic load ASP.NET User Controls in Visual Studio 2005 if I put the User Controls  in the App_Controls subfolder

Here is my test code:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

ucUserControl1 test = (ucUserControl1)LoadControl("~\\App_Controls\\ucUserControl1.ascx");

test.ProprtyValue="This is test code.";

this.PlaceHolder1.Controls.Add(test);

}

}

When I compile the code, I got the following compiling error:

The type or namespace name 'ucUserControl1' could not be found (are you missing a using directive or an assembly reference ).

Thanks.



Answer this question

How to dynamic load ASP.NET User Controls in Visual Studio 2005

  • JeriC_hsk

    Not work.
    So far, I need to add the following code in the ASPX file.
    <%@ Register Src="~/App_Controls/ucControlTest.ascx" TagName="ucControlTest" TagPrefix="uc" %>
    For dynamically load ASP.NET User Controls, this is not a good way. Do you guys have any better idea

  • MMuffett

    If you need to load a number of controlls, or you simply don't want to add the refrerence tag, then you'll have to use alternative methods to ensure type safety.
    If you only to load it, or do basic stuff, then you use
    Control c = LoadControl(...);
    or you could have one or more interfaces that the controls inherit( that is: use the interface pattern).
    It would be a good pratice to use interfaces here, because if you need to load a number of controls, chance is they share similarities, and you might be able to simplyify your logic.
    Last but not least, the use of interfaces futher the reuse of controls!

  • Rob Villahermosa

    Well then the interface pattern sounds to be dead on!
    Remember that interfaces can inherit multiple other interfaces, so you can bulid your own interface hirarcy. That way you can have the every class to implement the base interface, and you can spesialize the futher out the control tree you get.

  • Ribeye

    Sorry, I was a bit unclear.
    I meant, have you typed ASP into the code editor to see if the intellisense could find your type

  • Dtours

    Have you checked the ASP namespace, this is where the classes are compiled into( at least if you haven't spesified your own namespace)
  • melvin77

    You have to use the <@ Reference Control="~/App_Controls/ucControlTest.ascx" %>  Then it will work, and show up with intellisense!
  • GoPro

    Good idea! Thanks.
    Actually, I need to load a number of controls, those controls need to set some same user defined properties, and I don't want to add the refrerence tag.

  • Raghunandan Sharma

    Where can I set up the ASPX namespace. There is not namespace any more in the asp.net web site project in Visual Studio 2005.

    Thanks.

  • How to dynamic load ASP.NET User Controls in Visual Studio 2005