2
votes

I sometimes define Business Logic classes to "help" my ASPX code-behind classes. It makes the most sense to me to include them both in the code-behind file since they work together. However, I'd occasionally like to access the Business Logic classes from higher level classes defined in App_Code but they aren't automatically accessible outside of the file.

Thus, the question: it is easy to access classes defined in App_Code but how do I access classes defined elsewhere?

UPDATE: One other thing, the ASPX page class and the App_Code class are in the same namespace - that isn't the issue.

NOTE: I have taken the advice of those who have responded (thanks guys) and am refactoring to make class access easier. However, I don't think the question is actually answered yet (in the case of an ASP.NET Website project). I don't need the answer any more but, if someone could clarify what makes classes visible when they are outside of App_Code, it may well help someone else (or even me, down the road).

5

5 Answers

5
votes

Make sure you place your classes in a sensible namespace.

Place 'using' keyword in code behind files you would like to access them.

Or <%@ import if you are using them in inline code.

Put the dll that contains your classes in the /bin folder.

TBH I prefer to keep the separate library project in the same solution and have project reference in the Web probject. VS handles building and placing the dll for you.

2
votes

Some time while you create or add any file it has been create as the content file while it has to be compile file.

Please follow the process how to resolve this issue.

1) Right click on your class file in App_Code folder. 2) Click on properties. 3) Change Build Action from "" to Compile 4) Rebuild your Application.

This will work definitely

Tushar Tyagi

1
votes

I assume you mean you are defining a separate class inside the codebehind .cs file? What access modifiers are you giving them?

As above though, I'd generally have a separate project in the solution for this kind of thing (usually in a different namespace like MyApp.Web and MyApp.), and just reference it from there.

0
votes

You can also just create a standard folder in your project to access classes, and move them there, but you have to do some additional things to make it accessible to your project:

  • Ensure you don't name the folder any of the reserved terms (i.e. call it "AppCode", instead of "App_Code") if you are running into issues with the "App_Code" folder, after going through the rest of these bullets.
  • The classes should all have the same namespace as your code-behind.
  • Ensure the classes are made public, with public methods, if they are being called from other classes.
  • Include "using MyClass;" statements at the top of the code-behind/class files.
  • Make sure the class' Build Action property is set to Compile.
-2
votes

You can not access a class from another class in same code behind file as .net dsnt support multiple inheritance. but you can create you business logic class as inner class in main class and make its all functions public so that they can be call in main class.