15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Custom Controls<br />

Custom controls go a step beyond user controls in that they are entirely self-contained in <strong>C#</strong> assemblies,<br />

requiring no separate ASP.<strong>NET</strong> code. This means that you don’t need to go through the process of<br />

assembling a user interface (UI) in an .ascx file. Instead, you have complete control over what is written to<br />

the output stream, that is, the exact HTML generated by your control.<br />

In general, it will take longer to develop custom controls than user controls because the syntax is more<br />

complex, <strong>and</strong> you often have to write significantly more code to get results. A user control may be as simple<br />

as a few other controls grouped together, as you have seen, whereas a custom control can do just about<br />

anything short of making you a cup of coffee.<br />

Creating <strong>and</strong> referencing Custom Controls<br />

To get the most customizable behavior for your custom controls, you can derive a class from<br />

System.Web.UI.WebControls.WebControl. If you do this, you are creating a full custom control. Or,<br />

you can extend the functionality of an existing control, creating a derived custom control. Finally, you can<br />

group existing controls together, much as you did in the last section but with a more logical structure, to<br />

create a composite custom control.<br />

Whatever you create can be used in ASP.<strong>NET</strong> pages in pretty much the same way. All you need to do is<br />

place the generated assembly in a location where it can be found by the web site or application that uses it,<br />

<strong>and</strong> register the element names to use with the directive. For this location, you have two<br />

options: you can either put the assembly in the bin directory of the web site or application, or place it in the<br />

GAC if you want all web sites <strong>and</strong> applications on the server to have access to it. Or, if you are just using<br />

a user control on a single web site, you can just put the .cs file for the control in the App_Code directory<br />

for the site.<br />

The directive takes a slightly different syntax for custom controls:<br />

<br />

You use the TagPrefix option in the same way as before, but you don’t use the TagName or Src attributes.<br />

This is because the custom control assembly you use may contain several custom controls, <strong>and</strong> each of<br />

these will be named by its class, so TagName is redundant. Plus, because you can use the dynamic discovery<br />

capabilities of the .<strong>NET</strong> Framework to find your assembly, you simply have to name it <strong>and</strong> the namespace in<br />

it that contains your controls.<br />

In the previous line of code, you are instructing the program to use an assembly called<br />

PCSCustomWebControls.dll with controls in the PCSCustomWebControls namespace, <strong>and</strong> use the<br />

tag prefix PCS. If you have a control called Control1 in this namespace, you could use it with the<br />

ASP.<strong>NET</strong> code:<br />

<br />

The Assembly attribute of the directive is optional — if you have custom controls in the<br />

App_Code directory of your site, you can omit this, <strong>and</strong> the web site will look at code here for controls. One<br />

thing though — the Namespace attribute is not optional. You must include a namespace in code files for<br />

custom controls, or the ASP.<strong>NET</strong> runtime will not be able to find them.<br />

With custom controls, it is also possible to reproduce some of the control nesting behavior that exists in list<br />

controls, for example the way that you can nest controls inside a list control to populate<br />

the list control:<br />

<br />

The Happy Room<br />

The Angry Room<br />

The Depressing Room<br />

The Funked Out Room<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!