15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1186 ❘ ChaPTer 41 Asp.net feAtures<br />

Finally, in the largest section of this chapter, you look at ASP.<strong>NET</strong> AJAX. This technology is a powerful<br />

way to enhance the user experience. It enables web sites <strong>and</strong> applications to become more responsive<br />

by updating sections of a page independently, as well as streamlining the process of adding client-side<br />

functionality.<br />

Throughout much of this chapter, you will refer to a large example web site that includes all the techniques<br />

that you have seen in this <strong>and</strong> the previous chapter. This web site, PCSDemoSite, is available for download<br />

(along with the other code for this chapter) at www.wrox.com. The relevant sections of code are examined as<br />

necessary, <strong>and</strong> the additional code (mostly dummy content or simple code you have already seen) is left for<br />

you to examine at your convenience.<br />

user <strong>and</strong> CusTom ConTrols<br />

Sometimes, a given control doesn’t quite work as you would like it to, or perhaps one section of code,<br />

intended for reuse on several pages, is too complex in the h<strong>and</strong>s of multiple developers. In such cases, there<br />

is a strong argument for building your own controls.<br />

The .<strong>NET</strong> Framework provides an ideal setting for the creation of custom controls, using simple<br />

programming techniques. Every aspect of ASP.<strong>NET</strong> server controls is exposed for you to customize,<br />

including such capabilities as templating <strong>and</strong> client-side scripting. However, there is no need to write code<br />

for all these eventualities; simpler controls can be a lot easier to create.<br />

In addition, the dynamic discovery of assemblies that is inherent in a .<strong>NET</strong> system makes installation of<br />

web sites <strong>and</strong> applications on a new web server as simple as copying the directory structure containing<br />

your code. To make use of the controls you have created, you simply copy the assemblies containing those<br />

controls along with the rest of the code. You can even place frequently used controls in an assembly located<br />

in the global assembly cache (GAC) on the web server, so that all web sites <strong>and</strong> applications on the server<br />

have access to them.<br />

This chapter discusses two different kinds of controls:<br />

➤<br />

➤<br />

User controls <strong>and</strong> how to convert existing ASP.<strong>NET</strong> pages into controls<br />

Custom controls <strong>and</strong> how to group the functionality of several controls, extend existing controls, <strong>and</strong><br />

create new controls from scratch<br />

User controls are illustrated with a simple control that displays a card suit (club, diamond, heart, or spade),<br />

so that you can embed it in other ASP.<strong>NET</strong> pages with ease. We won’t go into too much depth for custom<br />

controls, although we show you the basic principles <strong>and</strong> direct you to more information beyond this book.<br />

user Controls<br />

User controls are controls that you create using ASP.<strong>NET</strong> code, just as you use in st<strong>and</strong>ard ASP.<strong>NET</strong><br />

web pages. The difference is that after you have created a user control you can reuse it in multiple<br />

ASP.<strong>NET</strong> pages.<br />

For example, say that you have created a page that displays some information from a database, perhaps<br />

information about an order. Instead of creating a fixed page that does this, you can to place the relevant<br />

code into a user control, <strong>and</strong> then insert that control into as many different web pages as you want.<br />

In addition, you can define properties <strong>and</strong> methods for user controls. For example, you can specify a<br />

property for the background color for displaying your database table in a web page, or a method to re-run a<br />

database query to check for changes.<br />

To start, you create a simple user control.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!