views:

1771

answers:

9

So I'm having problems when I try to publish the website. I'm in visual studio 2008 sp1.

I've got a bunch of user controls and on a few pages I'm using them programatically.

I've got a reference on the aspx page <%@ Reference Control="~/UserControls/Foo.ascx" %>

Then on the code behing I use

ASP.usercontrols_foo newFoo control = (ASP.usercontrols_foo)Page.LoadControl("~/UserControls/Foo.ascx");

If I navigate to the page it works fine, but when I goto publish the website I get a compile time error.

+2  A: 

Argh, I'm bleeding development hours on this same issue. Does anyone have a solution to this ?

BTW: It builds if you uncheck "Allow this precompiled site to be updatable" (Right-click on the website, choose Property Pages, MSBuild Option)

But I need the site to be updatable.

Riaz Hosein
1+, In the past, I was having the same problem, as I couldn't resolve it I used reflection. I would like to know how to resolve this issue as well.
Cleiton
A: 

It may have something to do with the specific type not being available. Can you change the control reference so the type and cast just use the base Control class:

Control control = Page.LoadControl("~/UserControls/Foo.ascx");
Mike Knowles
A: 

Yes, I can cast it to Control. But then I lose direct access to the methods on the usercontrol.

I know that I can access the methods by reflecting into the control, and I've successfully done that but that's far from ideal to access all the other user controls on the site.

Also, the follow-up error is that it cant find the usercontrols that on the markup

<%@ Register src="CategoryRows.ascx" tagname="CategoryRows" tagprefix="abc" %>
<abc:CategoryRows ID="CategoryRows" runat="server" />

Note that I can run the site successfully both locally and on the server if I essentially XCopy the entire site (source code and all). But Publish fails.

Riaz Hosein
Riaz, Are you publishing your website with "Use fixed naming and single page assemblies" checked?
Cleiton
I've tried with both "Use fixed naming and single page assemblies" checked and unchecked. ("Allow this precompiled site to be updatable" is checked in both cases)I get the same error:"The type or namespace name 'some_user_control_ascx' does not exist in the namespace 'ASP' (are you missing an assembly reference?)"
Riaz Hosein
A: 

I had this same problem - actually, in my case I could get it to compile and build my website, but it would blow up spectacularly when doing an automated build.

Try replacing your code as follows:

ASP.usercontrols_foo newFoo control = (ASP.usercontrols_foo)Page.LoadControl("~/UserControls/Foo.ascx");

with

USERCONTROLS_Foo newFoo control = (USERCONTROLS_Foo)Page.LoadControl("~/UserControls/Foo.ascx");

(Capitalization will be based on how you capitalized your directory name/control name - but in either case should highlight appropriately).

Bob Palmer
+1  A: 

Hi Joel and Riaz,

Were you able to overcome this problem. I am having the same issue. The code won't compile when "allow web site to be updatable" is selected. Changing the type name of the control did not help in my case.

Thanks, Bilge

Bilge Gul
the only thing I could do was this:"BTW: It builds if you uncheck "Allow this precompiled site to be updatable" (Right-click on the website, choose Property Pages, MSBuild Option)"
Joel Barsotti
A: 

Did anyone ever find a solution to this? I have no issue with it publishing in a non-updateable mode, but I'm getting the error when trying to use a Web Deployment project.

end-user
I still haven't solved this completely, but this might be of use: http://stackoverflow.com/questions/419849/web-app-compiling-error-asp-reference
end-user
A: 

Specify a namespace for user control (see http://stackoverflow.com/questions/1290592/dynamically-load-a-user-control-ascx-in-a-asp-net-website ).

cestbienmoi
A: 

Casting the user control may create many problem .my approach is to create a class (say control Class) put all the properties and method you need after casting 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
That isn't userfull if you need to load dynamically.Loading Dynamically requires Page.LoadControl, wich only returns Control.If anything I'd have to derive all my pages to specifiy the ability to load the different usercontrols.
Joel Barsotti
Even if the control is dynamically added . we can always cast the control to its base class .'--------------------------ctrl = Page.LoadControl("~/DashBoardControl/BroadCastView.ascx") PlaceHolder1.Controls.Add(ctrl)With DirectCast(ctrl, BraodCastView) If Not ctrl Is Nothing Then'-------------------------------------------------here BraodCastView is base class for BroadCastView.ascxthis never fails.
ratnesh
A: 

I've found a solution for it. If i'll declare controls in user defined namespace, then I can use controls directly without using ASP or referencing it into aspx page.

ratnesh