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.

You might also like...

Comments

About the author

James Yang Australia

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

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra