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:
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.
ASP.NET Controls Explained: Part 2/2
Components
You might also like...
About the author
James Yang
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.
ASP.NET books
-
20 Recipes for Programming MVC 3: Faster, Smarter Web Development
There's no need to reinvent the wheel every time you run into a problem with ASP.NET's Model-View-Controller (MVC) framework. This concise cookbook provides recipes to help you solve tasks many web developers encounter every day. Each recipe includes...
ASP.NET forum discussion
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Exporting data from a repeater to an Excel sheet
by TomBlack (28 replies)
-
Calculate Date of Birth from Age
by k.prashanth (7 replies)
-
How to get Hotmail Contacts
by salihagenter (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
ASP.NET podcasts
-
.NET Rocks: Jeff Fritz Knows One ASP.NET
Published 7 years ago, running time 0h54m
Carl and Richard talk to Jeff Fritz about how ASP.NET has evolved over the years. Jeff talks about the continued dominance of web forms in relation to MVC and how the different libraries can be used together. In fact, there's ONE ASP.NET, and everything: web forms, MVC, Web API, SignalR, all work.
ASP.NET jobs
-
.NET Web Developer at FinTech Startup (with traction)
FundApps in London, United Kingdom
Negotiable -
.NET Developer
sti-professionalservices in Detroit, United States
FOR ALL POSITIONS COMPENSATION IS NEGOTIABLE -
Web Developer at Growing Startup
FundApps in London, United Kingdom
-
Senior Software Engineers
LexisNexis in Cary, United States
Comments