views:

3350

answers:

4

Hi,

I am trying to dynamically load a user control in an asp.web site. However due to how asp.net websites projects are setup (I think), I am not able to access reach the type definition of the user control.

I get a message saying that my class HE_ContentTop_WebControl1 is: he type or namespace name 'HE_ContentTop_WebControl1' could not be found (are you missing a using directive or an assembly reference?)

Any idea how this could be made to work ? I have attempted using namespace but it seems to me that asp.net websites are not designed to work with namespaces by default. I would be interested in a non namespace approach.

TIA

public partial class HE_Default :
     System.Web.UI.Page   {  

    protected void Page_Load(object sender, EventArgs e)  
    {  
       var control = (HE_ContentTop_WebControl1)Page.LoadControl("~/ContentTop/WebControl1.ascx");         
    }
}
+4  A: 

Assuming the control exists in the same assembly as your web project, you need to add a reference directive in your .aspx file,

e.g:

<%@ Reference Control="~/Controls/WebControl1.ascx">

Keep in mind it often takes a few minutes (or sometimes a build) for IntelliSense to pick this up.

jscharf
Thanks very much. I had assumed the reference was not needed if I pointed to the location of the code file. It seemed a little redundant to add the file but again I made an assumption...
Tuka
If you're dealing with a lot of usercontrols I really wouldn't recommend this approach. In my experience the solution to add the usercontrols to a namespace and then load them dynamically is a lot more versatile.
Tchami
Yeah, it's a bit redundant but basically the reference directive is for ASP.NET so it can actually resolve the cast on Page.LoadControl at the appropriate time. Glad I could help!
jscharf
A: 

It can easily be done using namespaces. Here's an example:

WebControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="MyUserControls.WebControl1" %>

Notice that Inherits references the namespace (MyUserControls), and not just the class name (WebControl1)

WebControl1.ascx.cs:

namespace MyUserControls
{
    public partial class WebControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Notice that the class have been included in the namespace MyUserControls

Default.aspx.cs:

using MyUserControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var control = (WebControl1) Page.LoadControl("~/WebControl1.ascx");
    }
}

This approach potentially allow you to redistribute your user controls (or keep them in a separate project) without the hassle of referencing them in your .aspx files.

Tchami
+1  A: 

the reference is not enough using

<%@ Reference Control="~/Controls/WebControl1.ascx">

in the aspx file is just one part of the answer.

you need also to add the calssName in the User Control aspx file

<%@ Control ClassName="WebControl1" Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="AnySpaceName.DateSelector" %>

and then you can use the userontrol in your aspx file

AnySpaceName.WebControl1 WC = (AnySpaceName.WebControl1) Page.LoadControl("~/WebControl1.ascx");

A: 

Casting the user control this way may create many problems .my approach is to create a class (say control Class) put all the properties and method you need for casting in it and inherit this class from System.Web.UI.UserControl .Then in your user cotrol code file instead of System.Web.UI.UserControl user this control class .

now when ever you need casting, cast with this class only . it will be light casting as well.

ratnesh