Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 21, 2012 05:52 PM by AceCorban
Dec 21, 2012 05:03 PM|LINK
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 ;))
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)
//I can also call 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
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.