Donald Hansen
Donald Hansen

Reputation: 111

Type or namespace could not be found only on a publish

I have a web solution with Visual Studio 2010 and ASP.Net 4.0. In this solution there is a web site as well as a class library.

I added a new User Control to the site and was able to build everything. I was even able to publish with the new control. The issue arises when I try to reference the class name of that control in a different part of code. When I do this, I still am able to build / rebuild the entire solution. But when I try to publish, I get the error "type or namespace name could not be found."

When we publish, we always UNCHECK the box "Allow this precompiled site to be updatable" and we always CHECK the box "Use fixed naming and single page assemblies."

If I do a publish and UNCHECK the box "Use fixed naming and single page assemblies" it will publish just fine. But for some reason I can not get it to publish with that box CHECKED which is what I need to be able to do.

I have tried:

Cleaning the solution

Restarting Visual Studio

Restarting Computer

Clearing out .net temp files folder

Recreating the control

Putting everything in website into a namespace

Any help or insight would be greatly appreciated.

Thanks.

EDIT:

I figured it out. I was adding this new control dynamically. But I didn't realize it also needed registered on the page that I am using it on. After registering the control, everything worked as expected

Upvotes: 3

Views: 1707

Answers (3)

Juan_Mallorca
Juan_Mallorca

Reputation: 31

I tried that and still have the same problem when publishing, either with reference tag or register tag it doesn't publish. Only way to work by now is to uncheck the "updatable site"

Upvotes: 0

Devraj Gadhavi
Devraj Gadhavi

Reputation: 3611

When you want to use your web user control dynamically, as you use other .net controls like textbox, label etc. you need to follow these steps.

(1) Add a ClassName property in your control directive as below (in ascx file)

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

Note : Make sure the ClassName & Inherits both are same as class name in the CS file.

(2) Add a Reference directive on the page where you want to use your web user control dynamically

<%@ Reference Control="~/usercontrol/CustomControl.ascx" %>

Note : Reference directive is different then Register directive.

(3) Now you can add your user control dynamically anywhere on your page like below

ASP.CustomControl cuCtrl = (ASP.CustomControl)LoadControl("~/usercontrol/CustomControl.ascx");
//add dynamically created custom control to an existing panel or any container control on your page
pnlDemo.Controls.Add(cuCtrl);

Note : You can always include the ASP namespace in the using ;)

After this, you should not face any problem publishing even when checking the Allow this precomplied site to be updatable option.

Upvotes: 1

Donald Hansen
Donald Hansen

Reputation: 111

I figured it out. I was adding this new control dynamically. But I didn't realize it also needed registered on the page that I am using it on. After registering the control, everything worked as expected.

Upvotes: 0

Related Questions