Tuesday, 19 July 2011

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.

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

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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

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.

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>

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>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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.AspNetTutorials.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);
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment