Is it a good idea to put all code-behind in App_Code folder?

G

Guest

Hi, when I add a new page in my asp.net 2 project and put some controls
there, everything works fine until I move the code behind into App_Code
folder.--When I compile , it tells me that control "not exist in the current
context".

I thought putting code behind in App_Code folder is the recommendation for
asp.net 2. In order to do so, I just simply remove the codefile attribute in
@page.

Maybe my perception of putting all code in App_Code folder is totally wrong
from the beginning.Can you help me on this question?

Thanks a lot.
 
T

Teemu Keiski

Hi,

I wouldn't say it's a recommendation to place everything to App_Code.
Code-behind files are code-behind files and they serve mostly best as such
as separate files, unless you create some sort of base class for your
code-behind classes (such class would be a good example to place to
App_Code). I'd see App_Code a good place for helper classes etc when you
need component-like functionality & look and feel in presentation layer. And
it's handy because of the dynamic build.

You'd see the error because partial class feature works only with codefile
attribute e.g that separate companion class is created for the code-behind
class behind the scenes and this companion class contains the member
declarations which you don't have to put yourself in code-behind class.
These two are merged when compiled and the merged class is used as base
class for the class generated for aspx page.

If you don't use codefile attribute, you'd need to create the member for the
controls manually into the code-behind class.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top