Cannot get a handle on web UserControls in ASP 2.0

G

Guest

I'm converting some ASP web projects from VS 2003 to VS2005/ASP 2.0. The
worst problem I've run into is all references (in codebehind CS files) to
UserControls fail.

I get the error "The type or namespace 'MyUserControl' does not exist in the
namespace 'The.Correct.Namespace' (are you missing an assembly reference?)"
even though the user control in question is named 'MyUserControl' and is
explicitly wrapped in the namespace block 'namespace The.Correct.NameSpace
{'. And, obviously, it worked in the previous framework so it's not a typo.

Is there an approach for overcoming this conversion issue?

Thank you.

Greg Smalter
 
C

clintonG

The verbiage in the page directive has changed in beta 2. This article [1]
may help. What bothers me is there does not appear to be a central location
where changes have been documented. This of course is FUBAR.

I'm going to post to MSDN Product Feedback [2] and ask for a centralized
page or blog that tracks changes and ask you to do the same

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://west-wind.com/weblog/posts/2122.aspx
[2] http://lab.msdn.microsoft.com/ProductFeedback/Default.aspx
 
G

Guest

That explains a little about how the compilation models are different, but it
doesn't mention namespaces or how to address a transition. Good information
though, thanks.

I still need a way to make my existing ASP 1.1 framework web sites work in
ASP 2.0 by resolving this namespace issue and getting a handle on control
classes. It seems possible, because if I create a new web site and create a
control loose in the root, I can access it statically from the default page.
But if I create it in a folder, I can't. And there's no way for me to find
out what namespace it's using for the control in the folder, either. I need
to be able to statically access control classes no matter where they are,
like I used to.

Thank you.

Greg Smalter

clintonG said:
The verbiage in the page directive has changed in beta 2. This article [1]
may help. What bothers me is there does not appear to be a central location
where changes have been documented. This of course is FUBAR.

I'm going to post to MSDN Product Feedback [2] and ask for a centralized
page or blog that tracks changes and ask you to do the same

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://west-wind.com/weblog/posts/2122.aspx
[2] http://lab.msdn.microsoft.com/ProductFeedback/Default.aspx

Greg Smalter said:
I'm converting some ASP web projects from VS 2003 to VS2005/ASP 2.0. The
worst problem I've run into is all references (in codebehind CS files) to
UserControls fail.

I get the error "The type or namespace 'MyUserControl' does not exist in
the
namespace 'The.Correct.Namespace' (are you missing an assembly
reference?)"
even though the user control in question is named 'MyUserControl' and is
explicitly wrapped in the namespace block 'namespace The.Correct.NameSpace
{'. And, obviously, it worked in the previous framework so it's not a
typo.

Is there an approach for overcoming this conversion issue?

Thank you.

Greg Smalter
 
J

Juan T. Llibre

Clinton,

I think that what you're requesting was published
at MSDN in November 2004 :

http://msdn.microsoft.com/asp.net/whidbey/beta2update.aspx

The centralized page for ASP.NET Beta 2 information is at :

http://msdn.microsoft.com/asp.net/beta2/default.aspx

Bookmark it.

I go there at least weekly to get up to speed with changes/features.





clintonG said:
The verbiage in the page directive has changed in beta 2. This article [1] may help.
What bothers me is there does not appear to be a central location where changes have
been documented. This of course is FUBAR.

I'm going to post to MSDN Product Feedback [2] and ask for a centralized page or blog
that tracks changes and ask you to do the same

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://west-wind.com/weblog/posts/2122.aspx
[2] http://lab.msdn.microsoft.com/ProductFeedback/Default.aspx

Greg Smalter said:
I'm converting some ASP web projects from VS 2003 to VS2005/ASP 2.0. The
worst problem I've run into is all references (in codebehind CS files) to
UserControls fail.

I get the error "The type or namespace 'MyUserControl' does not exist in the
namespace 'The.Correct.Namespace' (are you missing an assembly reference?)"
even though the user control in question is named 'MyUserControl' and is
explicitly wrapped in the namespace block 'namespace The.Correct.NameSpace
{'. And, obviously, it worked in the previous framework so it's not a typo.

Is there an approach for overcoming this conversion issue?

Thank you.

Greg Smalter
 
G

Guest

The article basically mentions the changes necessary to the markup, which the
Visual Studio 2005 project upgrade utility has already done for me. It
doesn't mention namespaces at all. But it's a good article, thanks.

I'll try to describe the problem a little differently. I have a project in
which .cs files (not code-behind) existed in the same namespace as .ascx.cs
files (codebehinds for user controls). After upgrading to ASP 2.0, I have
access to the namespace, but only to the non-codebehind elements in it. None
of the classes that exist in codebehind files show up. It's as if they are
no longer part of the namespace, even though the namespace is explicitly
defined in those codebehind files (as was always the case in ASP 1.1).

I need a way to statically access classes and their static methods that
exist in user control codebehind files from other user control codebehind
files and from loose .cs code files, as was possible in ASP 1.1.

Thank you.

Greg Smalter.
 
S

Scott Allen

Hi Greg:

Unfortunately, this scenario has gotten insanely complex in 2.0, at
least as of beta 2. The problem is not a namespace issue, but a "which
assembly to reference" issue because web "projects" are no longer
projects.

Each web form, and each user control can compile into a single
assembly - think of each web form and each user control as a separate
class library project. An island unto themselves.

These posts might shed some insight:
http://odetocode.com/Blogs/scott/archive/2005/06/30/1889.aspx
http://odetocode.com/Blogs/scott/archive/2005/06/29/1882.aspx

To establish assembly references between pages and user controls
you'll need an @ Reference directive with a VirtualPath attribute
pointing to the control you want to reference.

Getting to a user control from a loose .cs file, which I assume is in
App_Code, is a bit more difficult. You'll need to define a 'stub'
class in App_Code for the user control. That is a class that your
UserControl class will then inherit from - this is the only way to
access ASPX/ASCX code from App_Code. In post beta 2 the runtime will
generate stub classes for you.

All of this complexity exists for people who like to randomly drop
files into a web application and have it just work...
 
J

Juan T. Llibre

Hi, Greg.

re:
None of the classes that exist in codebehind files show up.

That's strange. They do show up for me.

In my vb source ( sorry for using VB, but it should work the same for C# )
I have the following :

Namespace ObjetoDatosVB
Public Class RegistrosSQL
etc...

When I open my aspx file to work on it, and place
<%@ Import Namespace="ObjetoDatosVB" %>
at the top of my aspx, and then write

Dim RS as New

and hit the space bar, the available classes show up
and RegistrosSQL is available and is filled in by Intellisense.

Are your *.cs files in the App_Code directory ?

That's the only place where VS.NET Beta 2 will recognize them.
 
J

Juan T. Llibre

Hi, Scott.

re:
---000---
Getting to a user control from a loose .cs file, which I assume is in
App_Code, is a bit more difficult. You'll need to define a 'stub'
class in App_Code for the user control. That is a class that your
UserControl class will then inherit from - this is the only way to
access ASPX/ASCX code from App_Code.
---000---

I'll agree 100% with you if you limit the comments to ASCX code.

As explained in my previous reply, there's no problem in getting
Intellisense to recognize "standard" code-behind in class files,
as long as they are placed in the App_Code directory and the
Namespace is referenced with an Imports statement in the ASPX page.

I.O.W., accessing classes and their static methods that exist in user control
codebehind files needs the stubs or an @ Reference directive with a
VirtualPath attribute pointing to the control you want to reference,
as you well explain, but classes in "loose .cs code files" show up without
anything being required but placing them in the App_Code directory and
referencing the Namespace.
 
G

Guest

I think we're talking about opposite problems. You are illustrating that you
can access code files (non-codebehind) from an aspx page. My problem is that
I cannot access my aspx (or ascx) (codebehind) classes from a code file.
 
G

Guest

Scott,

Thank you very much for the good info. Unfortunately, it confirms that my
situation is prohibitively difficult. There is no way I can migrate a site
as large as the one I'm trying to migrate. Automatically generated stubs
that are coming later may help, but I'll still have to add hundreds of
@Reference directives to the markup of controls, if I understand all this
correctly.

Your comments bring up another complaint with the new ASP, though. Why does
everything have to be in App_Code? I had everything organized very nicely
with loose code files that support a particular controls in the same folder
as those controls, and now the auto-migrator tool has ripped them out and put
them in a parallel folder structure inside the App_Code folder. So now I
have to maintan parallel directory structures, and I can't find anything, and
all conceptual grouping is totally lost.

Furthermore, VS2005 doesn't even support adding a class file to any part of
a web site from what I can tell (even if I right click on the App_Code
folder). I have to create a text file and make the class myself. Am I
missing something?

Regardless of whether ASP 2.0 is better than ASP 1.1, it's not going to
matter if it's impossible to migrate any substantial site to it. I find
myself wishing for ASP 1.1 but with C# 2.0 support.

Thanks again.

Greg Smalter
 
S

Scott Allen

Hi Greg:

Yes, the only way to include a standalone .cs file is to keep it
inside of App_Code.

As for why? I think we have to look at the bigger picture that MS is
seeing. They want ASP.NET to appeal to as many developers as possible.
This includes appealing to the "Mort" persona, aka the business
developer who is not interested in technology or architecture but just
wants to solve a business problem. They also want to appeal to people
using ASP, PHP, and Perl.

In order to reach these people MS needs to sell ASP.NET as a dynamic
environment - an environment where a person can drop a new webform
into a folder and have it immediately generate content, or edit a
webform on the server and have the changes take effect immediately.

No mucking around with builds and compiles in these scenarios.
Everything, even code-behind, can compile on-the-fly.

I've been thinking about how to pull this off, and I keep ending up
with an implementation similar just like asp.net 2 is using:

Common code (App_Code) needs to be in a well-known location. The
dynamic compilation step can't traverse all of the folders and
sub-folders in a big application looking for .cs / .vb files, and all
of these files need to compile together into a single well known
assembly so that the types inside App_Code can see each other, and
also all the types in webform / usercontrol code can see public
App_Code types, too.

As for your migration problems - I'd suggest filing feedback with
Ladybug [1].

Based on your description, I'm not sure if you'll need an @ Reference
or not. If you are using the code-behind class for a usercontrol in
the code-behind class for a web form you'll need the directive. If you
are using the code-behind class for a usercontrol from code in
App_Code you'll need a stub class, or an interface from which to
derive your user control from. It's the only way to interact with the
types in webforms/user controls from App_Code.

[1] http://lab.msdn.microsoft.com/ProductFeedback/Default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top