You can either use inheritence or static methods. The inheritence option is good if the functions that you want to expose perform System.Web.UI.Page stuff (like Page_Load, etc), but those child classes will also have access to public and protected methods
in the BasePage as well:
public class BasePage : System.Web.UI.Page
{
protected virtual void Page_Load(object sender, EventArgs e)
{
//stuff you want all pages to do
}
protected void AnotherFunction()
{
//some other function not part of the Page Lifecycle.
}
}
public class OneOfMyPages : BasePage
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
//I can also call AnotherFunction:
AnotherFunction();
}
}
With the static class option, you can implement a class with static methods that are available to any class either includes or shares the namespace your static class is defined in:
public static class Functions
{
public static void GloballyAccessibleFunction()
{
}
}
//then, in one of your many pages:
//doing some stuff
//...
Functions.GloballyAccessibleFunction();
In both of these options, you will define these classes in a .cs file (not in an aspx.cs file). It is not code-behind for any particular page, it is simply a class.
"Dream as if you'll live forever, live as if you'll die today." --James Dean
szewciu
Member
7 Points
36 Posts
External file with functions
Dec 21, 2012 05:03 PM|LINK
Hi,
how to add (and then use) external file in project directory which includes custom functions used in *.aspx.cs files?
I mean like in php - e.g. functions.php file and in other files include/require_once('functions.php').
Is there any way to do this?
Or other way - how to use functions from one .aspx.cs file in the other one (of course without copying all of them ;))
AceCorban
Star
12318 Points
2269 Posts
Re: External file with functions
Dec 21, 2012 05:52 PM|LINK
You can either use inheritence or static methods. The inheritence option is good if the functions that you want to expose perform System.Web.UI.Page stuff (like Page_Load, etc), but those child classes will also have access to public and protected methods in the BasePage as well:
public class BasePage : System.Web.UI.Page { protected virtual void Page_Load(object sender, EventArgs e) { //stuff you want all pages to do } protected void AnotherFunction() { //some other function not part of the Page Lifecycle. } } public class OneOfMyPages : BasePage { protected override void Page_Load(object sender, EventArgs e) { base.Page_Load(sender, e); //I can also call AnotherFunction: AnotherFunction(); } }With the static class option, you can implement a class with static methods that are available to any class either includes or shares the namespace your static class is defined in:
public static class Functions { public static void GloballyAccessibleFunction() { } } //then, in one of your many pages: //doing some stuff //... Functions.GloballyAccessibleFunction();In both of these options, you will define these classes in a .cs file (not in an aspx.cs file). It is not code-behind for any particular page, it is simply a class.