

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Sharing Code Between Pages using ASP.NET 2.0 and C#
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.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Please build a CustomClassVB class in the folder Subdirectory. The code as following:
Imports Microsoft.VisualBasic
Public Class CustomClassVB
Public Function GetMessage(ByVal name As String) As String
Return "Hello from VB " & name End Function End Class |
Secondly, build a CustomClass. The code as following:
using System;
/// <summary> /// CustomClass summary /// </summary>
public class CustomClass {
public String GetMessage(String input) {
return "Hello " + input; } } |
Thirdly,build a web.config file Site.master. The code as following:
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
<?xml version="1.0" encoding="utf-8"?> <configuration>
<system.web>
<compilation> <codeSubDirectories>
<add directoryName="Subdirectory"/> </codeSubDirectories> </compilation> </system.web> </configuration> |
The front end SharingCodeBetweenPagesCsharp.aspx page looks something like this:
<div align="center"> <b>Enter Your Name:</b> <asp:TextBox ID="TextBox1" Runat="server"/> <asp:Button ID="Button1" Text="Click Me" Runat="server" OnClick="Button1_Click"/> <br /> <br /> <asp:Label ID="Label1" Runat="server" /> <br /> <asp:Label ID="Label2" Runat="server" /> </div> |
The flow for the code behind page is as follows
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
// This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com // Visit http://www.DotNetTutorials.com for more ASP.NET Tutorials } protected void Button1_Click(object sender, EventArgs e) {
CustomClass c = new CustomClass(); Label1.Text = c.GetMessage(TextBox1.Text); CustomClassVB c2 = new CustomClassVB(); Label2.Text = c2.GetMessage(TextBox1.Text); } } |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!