0
votes

I am trying to dynamically create a user controls in Visual Studio 2012/ASP.Net/C# but my project does not recognise the type name of my user control when I try to use it in the code behind of any other aspx or ascx page. I've tried rebuilding it and also removing namespaces and it seems like it should just work but it doesn't!

The top line of my ascx file is this:

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

The corresponding codebehind looks like this:

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

    }
}

I try to declare in instance of it in the code behind of a blank aspx page like this:

AG_Controls_WebUserControl test = new AG_Controls_WebUserControl();

And I just get :

Error 3 The type or namespace name 'AG_Controls_WebUserControl' could not be found (are you missing a using directive or an assembly reference?)

It's been a long day and I am pretty tired but this shouldn't be rocket science, but I can't see what the problem is. Help much appreciated!

1
Have you included the namespace of the usercontrol? - GregoryHouseMD
No there's no namespace declared in the code behind so it should be reference-able from the rest of the project - Damo
if you have not included a name space which you may want to do, have you tried also changing CodeFile="WebUserControl.ascx.cs" to CodeBehind="WebUserControl.ascx.cs" look at the web appliation's properties page and see if there is a default NameSpace use that one - MethodMan
Just try to create a new usercontrol and create that one dynamically. If that works, just copy/paste the code :) you said it was a long day so it might me something too silly to notice - GregoryHouseMD
That's what I did with this one! The actual user control is something more complicated so I just created a standard empty control to try and narrow down what the problem was. I also tried changing 'CodeFile' to 'CodeBehind' but then the control itself wont compile, I just receive the error: 'Error 1 Could not load type 'AG_Controls_WebUserControl'.' - Damo

1 Answers

2
votes

You forgot to include the reference to the control in the ASPX page. You still need to include that reference if you are dynamically creating the control in the code behind.

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>