How to reuse the same functions in different .aspx files or create a library of functions?

Currently my asp.NET site project consists of 5.aspx files. I defined the same functions in every .aspx file. I want to create my own library / file of functions that I could include in my .aspx scripts.

While searching for a solution, I found this dynamically including files in ASP.NET , which is used to dynamically include HTML and client scripts in .aspx. So this is not what I need.

+5
source share
5 answers

You can easily create a library in ASP.NET.

( , , )

ASPX , , , , , , .

ASPX:

<%@ Import Namespace="mylibrarynamespace" %>

:

using mylibrarynamespace;

, .

+7

, ? .

+2

, . , .

, :

+2

.cs, App_Code. , .

+1

! - asp.net!

1) App_Data: ( > > ASP.Net) ( > > )

important! when you add these classes, you need to switch the assembly action to “compile” instead of “content” in the file properties, otherwise your code name will not recognize the imported namespaces.

2) Inside the class, create methods:

namespace applicationName.App_Code
   {
       public class ClassName
           {
               public string classMethod()
                  {
                    return "hello";
                  }
           }
   }

3) In the code form of any form, include this class header:

using applicationName.App_Code; //add this

4) In the code code of this form, create an instance of the class:

ClassName classInstanceName = new ClassName(); //add this

5) Call the method in your codebehind form

classInstanceName.classMethod()
0
source

All Articles