At the moment my website project in asp.NET consists of 5 .aspx files. I have defined the same functions in every .aspx file. What I want is a way to create my own library/file of functions, which I could include in my .aspx scripts.
While searching for solution I've only found this Dynamically Including Files in ASP.NET Which is used to dynamically include HTML and client-side scripts in .aspx. So it's not what I need.
You can create a library very easily in ASP.NET.
Decide on a namespace and create new classes under that namespace (generally each class in a separate file but it is not enforced)
If your ASPX files don't have code-behind I would recommend to add it, however regardless of whether there is a code-behind or not you would need to make sure you include the library files' namespace before you can access the library classes and their methods.
Syntax for including a namespace on a ASPX file is:
<%@ Import Namespace="mylibrarynamespace" %>
In the code-behind is:
using mylibrarynamespace;
Hope it helps.
How about making a base class from which all of your page classes derive? That seems like the simplest way to go.
There are a lots of approaches you can take to sharing code between pages. It really depends on what type of functionality you are trying to achieve.
Here are some that I've encountered in the past:
Just create a .cs file is drop it in your App_Code directory. Make your class and appropriate functions public, and you're good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With