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 - Performance - Using Shared Code in ASP.NET and C#
Using Shared Code in ASP.NET and C#


ASP.NET Performance Tutorial

This tutorial shows how we can create our own classes and use shared code between different pages on our website. C# version.


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

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

Looking for more ASP.NET Tutorials? Click Here!

Visual Studio allows you to create classes in separate files to be used in multiple pages on your website.
Classes you create are stored in the App_Code folder and can be in any language you prefer. For this tutorial, we'll create a sample class:

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;

/// <summary>
/// Summary description for TestClass1
/// </summary>
public class TestClass1
{
public TestClass1()
{

}
private string testStringValue;
public string testString
{
get
{
return testStringValue;
} set
{
testStringValue = value;
}
}
}

When called, the class can either store data or return data. We create a text box and a button to show how we can store data in a variable in a separate class, and then a label to show how we can retrieve that same data.
The ASPX page which is using the class function:

<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />&nbsp;<br />
<asp:Label ID="Label2" runat="server" Text="Yout typed in: " Visible="False"></asp:Label>
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>

The code-behind should look something like this:

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)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
TestClass1 tc = new TestClass1();
tc.testString = TextBox1.Text;
Label1.Text = tc.testString;
Label2.Visible = true;
}
}

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks