App_Code & bin

R

rn5a

Is putting a VB class file in the special directory named App_Code the
same as relocating the VB class file from the App_Code directory to
another directory & then using the VBC tool, compiling the VB class
file into a DLL & putting the DLL in the bin directory?

Though while running an ASP.NET app using either of the 2 approaches
doesn't make any difference, Visual Web Developer 2005 Express Edition
behaves erratically sometimes if the VB class file resides in the
App_Code directory & the corresponding DLL doesn't exist in the bin
directory.

I have a user control named MyUC.ascx. This file only contains the UI
elements (like TextBoxes, Labels etc.) & the entire logic resides in a
code behind named MyUC.ascx.vb which exists in the App_Code directory.
I register the user control in an ASPX page named MyPage.aspx (which
resides outside the App_Code directory) with the following Register
directive

<%@ Register TagPrefix="UC" TagName="UCtrl" Src="MyUC.ascx" %>

& add the following lines after the Register directive:

<script runat="server">
Public UC1 As MyUC_ascx
Public UC2 As MyUC_ascx

Sub Page_Load(....)
'some code
End Sub
</script>

Now when the mouse is hovered over any of the 2 lines which declare the
2 variables UC1 & UC2 (the 2 "Public" lines) in VWD, then VWD says

Type 'MyUC_ascx' is not declared.

But if I relocate the code behind MyUC.ascx.vb to some directory other
than the App_Code directory & then compile MyUC.ascx.vb into a DLL in
the bin directory using VBC, then VWD no longer reflects the error when
the mouse is moved over the 2 lines which declare UC1 & UC2.

Is this some sort of bug in VWD or does the CLR behave differently when
a VB class file resides in the App_Code directory (& no corresponding
DLL exists in the bin directory) and when the VB class does not reside
in the App_Code directory & the VB class file is compiled into a DLL in
the bin directory?
 
K

Karl Seguin

Yes, it's pretty much the same.

I don't like the new model for any type of modestly complicated site. It's a
nightmare for many reasons, including the one you are running into.

There's a Web Application Project addon for VS.NET 2005 which makes it work
a lot more like it did in 2003. I use it almost exlusively and would suggest
you look at it:
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx

Karl
 
M

Mark Rae

I don't like the new model for any type of modestly complicated site. It's
a nightmare for many reasons, including the one you are running into.
Agreed.

There's a Web Application Project addon for VS.NET 2005 which makes it
work a lot more like it did in 2003. I use it almost exlusively and would
suggest you look at it:
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx

Since I first downloaded it, I've never used the "old" way...
 
R

rn5a

Actually I am using Visual Web Developer 2005 Express Edition to create
& edit ASP.NET projects. Would both of suggest using VS.NET 2005
instead?
 
M

Mark Rae

Actually I am using Visual Web Developer 2005 Express Edition to create
& edit ASP.NET projects. Would both of suggest using VS.NET 2005
instead?

I certainly would. Of course, there is a price difference... :)
 
J

Juan T. Llibre

re:
the entire logic resides in a code behind named
MyUC.ascx.vb which exists in the App_Code directory

There's your problem.

The App_Code directory is not meant to be a repository for code-behind.
It's only a repository for helper classes.

Put your code-behind sources in the same directory as your page sources.
Or...compile to a DLL and don't use the source code-behind at all.
 
J

Juan T. Llibre

For stand-alone developers, there's practically no difference.
The differences in VS 2005 mostly apply to networked ( shared ) development.
 
R

rn5a

There's your problem.
The App_Code directory is not meant to be a repository for code-behind.
It's only a repository for helper classes.

Put your code-behind sources in the same directory as your page sources.
Or...compile to a DLL and don't use the source code-behind at all.

Juan, as per your advice, I removed all the code behind files from the
App_Code directory & relocated them to the directory that houses the
page sources but it's causing problems. Assume that an ASPX page, named
Shop.aspx, resides in C:\Inetpub\wwwroot\ASPX\Shop. I cut the code
behind of Shop.aspx named Shop.aspx.vb from
C:\Inetpub\wwwroot\ASPX\Shop\App_Code & pasted it in
C:\Inetpub\wwwroot\ASPX\Shop directory. The public class in
Shop.aspx.vb is named ShopCB. I added the Inherits attribute to the
Page directive in Shop.aspx like this:

<%@ Page Language="VB" Explicit="True" Inherits="ShopCB" %>

but when I run the app, I get the following error:

Could not load type 'ShopCB'.

pointing to the above Page directive.

Shop.aspx.vb also imports a few user-defined namespaces. For e.g. there
is a class file named MyShop.vb (which is a helper class) in the
App_Code directory which has the namespace NSShop. There is a line in
Shop.aspx.vb which imports this namespace NSShop using the following
line.

Imports NSShop

When I try to compile Shop.aspx.vb into a DLL, I get the following
error:

Namespace or type specified in the Imports 'NSShop' doesn't contain any
public member or cannot be found. Make sure the namespace or the type
is defined and contains at least one public member. Make sure the
imported element name doesn't use any aliases.

I am sure that the namespace NSShop exists because when I type the word
"Imports" followed by a space in Shop.aspx.vb, then intellisense lists
NSShop as one of the namespaces. So I am sure the namespace NSShop
exists. Moreover namespace NSShop does contain public members (in fact,
more than one). I am not using any aliases as well. So why am I
encountering this error when I am trying to compile Shop.aspx.vb?

Note that if I relocate Shop.aspx.vb in the App_Code directory, then
the app works fine without any problems.

This is really driving me nuts.....
 
J

Juan T. Llibre

re:
<%@ Page Language="VB" Explicit="True" Inherits="ShopCB" %>
but when I run the app, I get the following error:
could not load type 'ShopCB'.
pointing to the above Page directive.


You're missing the CodeFile reference for ASP.NET 2.0 :

Basic syntax differences between ASP.NET 1.1 and ASP.NET 2.0 :

a. ASP.NET 1.1 page definition
a. <%@ page codeBehind="webform1.aspx.cs" inherits="WebForm1" %>

b. ASP.NET 1.1 code-behind class definition
a. public class WebForm1 : System.Web.UI.Page

c. ASP.NET 2.0 page definition
a. <%@ page codeFile="webform1.aspx.cs" inherits="WebForm1" %>

d. ASP.NET 2.0 code-behind class definition
a. public partial class WebForm1 : System.Web.UI.Page




=
There's your problem.

The App_Code directory is not meant to be a repository for code-behind.
It's only a repository for helper classes.

Put your code-behind sources in the same directory as your page sources.
Or...compile to a DLL and don't use the source code-behind at all.

Juan, as per your advice, I removed all the code behind files from the
App_Code directory & relocated them to the directory that houses the
page sources but it's causing problems. Assume that an ASPX page, named
Shop.aspx, resides in C:\Inetpub\wwwroot\ASPX\Shop. I cut the code
behind of Shop.aspx named Shop.aspx.vb from
C:\Inetpub\wwwroot\ASPX\Shop\App_Code & pasted it in
C:\Inetpub\wwwroot\ASPX\Shop directory. The public class in
Shop.aspx.vb is named ShopCB. I added the Inherits attribute to the
Page directive in Shop.aspx like this:

<%@ Page Language="VB" Explicit="True" Inherits="ShopCB" %>

but when I run the app, I get the following error:

Could not load type 'ShopCB'.

pointing to the above Page directive.

Shop.aspx.vb also imports a few user-defined namespaces. For e.g. there
is a class file named MyShop.vb (which is a helper class) in the
App_Code directory which has the namespace NSShop. There is a line in
Shop.aspx.vb which imports this namespace NSShop using the following
line.

Imports NSShop

When I try to compile Shop.aspx.vb into a DLL, I get the following
error:

Namespace or type specified in the Imports 'NSShop' doesn't contain any
public member or cannot be found. Make sure the namespace or the type
is defined and contains at least one public member. Make sure the
imported element name doesn't use any aliases.

I am sure that the namespace NSShop exists because when I type the word
"Imports" followed by a space in Shop.aspx.vb, then intellisense lists
NSShop as one of the namespaces. So I am sure the namespace NSShop
exists. Moreover namespace NSShop does contain public members (in fact,
more than one). I am not using any aliases as well. So why am I
encountering this error when I am trying to compile Shop.aspx.vb?

Note that if I relocate Shop.aspx.vb in the App_Code directory, then
the app works fine without any problems.

This is really driving me nuts.....
 
R

rn5a

OK....now I get it. You have hit the nail on the head......I was
missing the CodeFile Attribute in the @Page directive.

Juan, you had adviced in one of your previous posts that App_Code is
meant for helper class files & not for code-behinds - code behinds
should exist in the same directory where the actual ASPX page
resides.......that's fine but after going through a heap of
permutations & combinations, I have concluded that if the code behind
files are also placed in the App_Code directory, then there's no need
to use the CodeFile attribute in the @Page directive. Agreed that
App_Code is not meant for code behinds but what's the harm in placing
code behinds in the App_Code directory? Are there any disadvantages of
doing so?

Another interesting thing I found out is assuming that the code behind
resides in the same directory as the ASPX page, if instead of using the
CodeFile attribute in the @Page directive, the Src attribute is used,
then all the UI elements that have been used in the ASPX page have to
be declared explicitly in the code behind. For e.g. if the ASPX page
uses 2 TextBoxes whose IDs are txt1 & txt2 and one Button whose ID is
btnSubmit, then if the Src attribute is used in the @Page directive
instead of the CodeFile attribute, then the 2 TextBoxes & the Button
have to be explicitly declared in the code behind like this:

Public Class MyClass : Inherits Page
Public txt1 As TextBox
Public txt2 As TextBox
Public btnSubmit As Button
.........
.........
.........
End Class

But if the CodeFile attribute is used in the @Page directive instead of
the Src attribute, then declaring the UI elements in the code behind as
shown above generates the following error:

'txt1' is already declared as 'Protected Dim WithEvents txt1 As
System.Web.UI.WebControls.TextBox' in this class.

What causes this error when CodeFile is used instead of Src in the
@Page directive?
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top