Library tutorials & articles

ASP.NET Controls Explained: Part 2/2

Components

Firstly, let's find out what components really are. Put simply, components are namespaces that contain classes with methods and properties that can be reused. In classical ASP, the primary use of components was to increase the speed of ASP pages, because COM components were compiled into machine code, thus making them quicker to execute than ASP pages. In ASP.NET, both pages and controls are compiled and cached, so what are the benefits of using components over inline ASP.NET code?

Well, let's think about this situation: you’ve just made a program that has encrypting and decrypting capabilities using secret algorithms. You want to sell the encrypting section of the program to a third party so that they can integrate it in to their program, but you don't want them to know how the encryption methods work, you want the method to act as a black box, accepting a value, and returning that value encrypted.

By using components, you can provide these companies with the required encryption method without actually letting them see your source code or change it in any way. Using components is a great way to reduce the chance of someone plagiarising your source code and methods that you've worked hard to create.

Let's look at an example of creating and using a component. We will create a component containing one method called "Add", which will take two integer parameters and return the sum of these integers.

Enter the following code into notepad and save it as comp.cs:

using System;

namespace devArticles
{
public class devComp
{
public int Add(int a, int b)
{
return (a+b);
}
}
}


Create another file named comp.aspx and enter the following code into it:

<%@ Import Namespace="devArticles"%>

<html>
<head>
<script language= "c#" runat ="server">
void Page_Load()
{
devComp objComp = new devComp();
label1.Text = Convert.ToString(objComp.Add(1,2));
}
</script>
</head>
<body>
<asp:Label id="label1" runat="server"/>
</body>
</html>


Before we can run our .aspx page in a web browser, we have to compile our custom control. Enter the following commands at the command line:

md bin
csc /t:library /out:bin\ comp.dll /r:System.dll comp.cs


When I ran comp.aspx in my web browser it looked like this:

Testing our custom control in a web browser

The code for our custom controls is fairly straight forward, so let's just take a look at some important parts:

namespace devArticles
{
public class devComp
{
public int Add(int a, int b)
{
return (a+b);
}
}
}


The code above creates a new public class named devComp. DevComp contains one public function called "Add" that takes two parameters and returns an integer, as described earlier. Both the class and function are contained within the "devArticles" namespace.

<%@ Import Namespace = "devArticles"%>

The line above simply imports the "devArticles" namespace, and is equivalent to "using devArticles;" in a typical C# application.

devComp objComp = new devComp();
label1.Text = Convert.ToString(objComp.Add(1,2));


In our .aspx page, we create a new instance of our devComp class and set the value of the Text member for our label control to the string value of the result returned by calling the "Add" method (In our example we pass in 1 and 2, which returns the value 3). Components are a great addition to any ASP.NET web site, and they provide an easy way to encapsulate your code into compiled objects, which can be instantiated, but never have their source code viewed.

Comments

  1. 13 Jul 2005 at 15:38

    Can i print  code in main page head tag by overriding render without adding any extra item in head tag.

  2. 04 Apr 2002 at 21:32
    I find this article really helpful for those who are starting to get familiar to asp controls.
    Thanks!
  3. 01 Jan 1999 at 00:00

    This thread is for discussions of ASP.NET Controls Explained: Part 2/2.

Leave a comment

Sign in or Join us (it's free).

James Yang James is a student at Georgia Institute of Technology, majoring in Computer Science. He is an MCSE, MCDBA, MCSA and CCNA.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!