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 End Class Return "Hello " & name End Function |
Thirdly,build a web.config file Site.master. The code as following:
| <?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> </configuration><compilation> </system.web><codeSubDirectories> <add directoryName="Subdirectory"/> </codeSubDirectories></compilation> |
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 End Class Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As New CustomClass End SubLabel1.Text = c.GetMessage(TextBox1.Text) Dim c2 As New CustomClassCsharp Label2.Text = c2.GetMessage(TextBox1.Text) 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 End Sub' Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials |
No comments:
Post a Comment