How to Fix Visual Studio Error 'System.Web.UI.UserControl' does not contain a definition for

by

Recently, I decided to begin converting my Visual Studio.net Web Site Projects (WSP) to Web Application Projects (WAP) to take advantage of some of the new Web Deployment features in Visual Studio.net 2010 (see Vishal Joshi's blog for a run through of WSP vs. WAP).  After converting my Web Site Project to a Web Application Project using the step by step directions here, I ran into a number of errors on project Build.  The most common of the errors that I ran into were:

'System.Web.UI.UserControl' does not contain a definition for 'CurrentCategoryId' and no extension method 'CurrentCategoryId' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?)

Essentially, the problem that I was having was that I could not access properties and methods of a UserControl (.ascx) from a Webform page (.aspx).  What I came to realize, is that when the WSP was converted to a WAP, Visual Studio does not wrap the 'namespace' around webforms or usercontrols 'public partial class', but keeps the .aspx files and .cs files similiar to how they are created in a Web Site Project (WSP).  However, if you create a new ASP.NET webform page or usercontrol in a Web Application Project (WAP) you will see a namespace wrapped around the public partial class that looks something like: 

namespace MyWebsiteBuilderWAP.articles.default

So to get things working without any errors I needed to make the following changes to (.aspx and .aspx.cs files)...

In your .aspx.cs file change from:

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

---- Change To ----

namespace MyWebsiteBuilderWAP.admin.articles {

public partial class previewarticle : System.Web.UI.Page { // code here }

}

Note: Above... 'MyWebsiteBuilderWAP' is the name of my Web Application Project, so you will want to change the namespace accordingly to your Project.

Now at the top of the .aspx file change:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="admin_articles_previewarticle" Title="Article" Codebehind="previewarticle.aspx.cs" %>

---- Change To ----

<%@ Page Language="C#" AutoEventWireup="true" Inherits="MyWebsiteBuilderWAP.admin.articles.previewarticle" Title="Article" Codebehind="previewarticle.aspx.cs" %>

You may also want to make the same namespace / Inherits changes to your UserControl .ascx file and .ascx.cs files as well, but I found that the errors go away just by changing this in .aspx and .aspx.cs files.  

Note: Making the changes outlined above to my .aspx pages also solved other error messages that were coming from AjaxToolkit controls on my pages that had error messages along the lines of: "The name 'CollapsiblePanelExtender1' does not exist in the current context"

 

 


0
0

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Comment:  
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts


To display line numbers in a Visual Studio.net 2008 code window do the following: Open Visual Studio.net On the Menu bar, Select 'Tools' -> 'Options' In the Options pop-up window, Select 'Text Editor' -> 'All Languages' > 'General'. Note: If you...  more »

The default settings in Visual Studio 2010 Professional do not automatically show the Solution file by default in the Solution Explorer sidebar. This can be a bit of a problem if you are trying to add another project to your existing solution. To get the...  more »