Building Components

I l @ ve RuBoard

Building Components

As we learned in the beginning of the chapter, another custom tool you can build with .NET is a component. A component is similar to a control, but doesn't inherit from the Control class and typically doesn't render any UI. A common use for components is to encapsulate business logic or data access logic. In this section, we will create a simple SalesTax component and demonstrate how to use it in an ASP.NET page. For more on components, refer to the IBuySpy case study in Chapter 14.

To create a component, we simply write a class just as we did with our initial HelloWorld control. However, for a component we don't need to inherit from anything (although we can if we choose to), so for this example just declaring the class is sufficient. The complete listing for our SalesTax component is in Listing 12.15.

Listing 12.15 SalesTax.cs
 namespace ASPNETByExample {     public class SalesTax     {         static double taxRate = 0.06;         public static double computeTax(double price)         {             return price * taxRate;         }     } } 
I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net