Is it possible to create a function that can be called by multiple different pages in asp.net?
Say for example I have a method that takes two int parameters and adds them together and spits out the result
public int test(int a, int b)
{
return a+b;
}
If I wanted to use this function on three different pages, how would I do it?
notes: using C# .NET 4 Entity Framework
This method can be made static and you can put such methods into a static class like this.
public static class Utils
{
public static int test(int a, int b)
{
return a+b;
}
}
can call it from any page.
int result = Utils.test(someA, someB);
The other option is, if it's some page oriented function, you could create your own class that extends Page
class and add function there and if you make all your pages extend this custom class, the functions will be available.
public class MyPage : Page
{
public int test(int a, int b)
{
return a+b;
}
....
}
This method can be made static and you can put such methods into a static class like this.
public static class Utils
{
public static int test(int a, int b)
{
return a+b;
}
}
can call it from any page:
int result = Utils.test(someA, someB);
Two options that make the most sense to me would be to:
Common
or Utils
. BasePage
that inherits from System.Web.UI.Page
, and have your three pages inherit from the BasePage, which contains your test() method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.