Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Advanced Technologies - Sharing Code Between Pages using ASP.NET 2.0 and VB.NET
Sharing Code Between Pages using ASP.NET 2.0 and VB.NET


ASP.NET Advanced Technologies Tutorial

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.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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;
}
}

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Subdirectory"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.AspNetTutorials.com for more ASP.NET Tutorials
End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks