Home arrow ASP.NET arrow Page 3 - ASP.NET Controls Explained: Part 2/2
ASP.NET

ASP.NET Controls Explained: Part 2/2


In this, the final article of the two part series relating to ASP.NET controls, James teaches us about custom controls and components. He talks about creating them, as well as compiling them from the command prompt using the C# compiler. He also provides two ASP.NET examples that demonstrate how to both create and integrate custom controls and components into our ASP.NET pages.

Author Info:
By: James Yang
Rating: 5 stars5 stars5 stars5 stars5 stars / 31
February 04, 2002
TABLE OF CONTENTS:
  1. · ASP.NET Controls Explained: Part 2/2
  2. · Custom Controls VS Components
  3. · Components
  4. · Conclusion

print this article
SEARCH DEVARTICLES

ASP.NET Controls Explained: Part 2/2 - Components
(Page 3 of 4 )

If you've developed with classical ASP, then you will understand the pain you have to go through to develop and integrate components into your ASP web site using tools and technologies such as regsvr32 and COM+. ASP.NET has revolutionized the way components are created and used, and they are extremely easy to develop and implement.

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.
blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials