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:
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.
Related articles
Related discussion
-
Profile Class does not work after Translation
by converter2009 (1 replies)
-
what is the SQL Server Provider
by hayperaktib (1 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
sharepoint calendar web part with events from sql table
by converter2009 (2 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
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.
Can i print code in main page head tag by overriding render without adding any extra item in head tag.
Thanks!
This thread is for discussions of ASP.NET Controls Explained: Part 2/2.