This tutorial will show you how to share code between pages using ASP.NET 2.0 and C#. Although you can place code inside each page within your site (using the inline or code-behind separation models described in the previous section), there are times when you will want to share code across several pages in your site. It would be inefficient and difficult to maintain this code by copying it to every page that needs it. Fortunately, ASP.NET provides several convenient ways to make code accessible to all pages in an application.

The following example demonstrates an App_Code directory partitioned to contain files in both the VB and C# languages.
We will use default namespace in the example.

Please build a CustomClassVB class in the folder Subdirectory. The code as following:

Secondly, build a CustomClass. The code as following:

Thirdly,build a web.config file Site.master. The code as following:

The front end SharingCodeBetweenPagesCsharp.aspx page looks something like this:

The flow for the code behind page is as follows

Download Source Files