
Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Sharing Code Between Pages using ASP.NET 2.0 and VB.NET
This tutorial will show you how to share code between pages using ASP.NET 2.0 and VB.NET.
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.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Please build a CustomClassCsharp class in the folder Subdirectory. The code as following:
using System;
/// <summary> /// CustomClassCsharp summary /// </summary>
public class CustomClassCsharp {
public String GetMessage(String input) {
return "Hello from C# " + input; } } |
Secondly, build a CustomClass. The code as following:
Imports Microsoft.VisualBasic
Public Class CustomClass
Public Function GetMessage(ByVal name As String) As String
Return "Hello " & name End Function End Class |
Thirdly,build a web.config file Site.master. The code as following:
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
<?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
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As New CustomClass Label1.Text = c.GetMessage(TextBox1.Text)
Dim c2 As New CustomClassCsharp Label2.Text = c2.GetMessage(TextBox1.Text) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 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 End Sub End Class |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!