Implement user-defined controls

May include but is not limited to: deciding whether to use a user/composite, extended, or custom control; creating a user/composite control; extending from an existing control

This is another area where the WPF control model and the ASP.NET control model overlaps closely. As you can remember (it was posted on this blog) in ASP.NET there are custom controls (they can be installed into the toolbox and dragged from there to the form, and they are the basic unit of encapsulation) and user controls, which are made for a single application, by composing some existing controls together. This stands for WPF as well.

MS exams aren’t very good at custom controls (I think they are too complicated to test). This objective has it written too that you don’t have to implement a custom control by writing code. Huh, we got our asses saved, no code writing! Just stick close to XAML and no harm can come to you. So we have to decide when we will use which type of controls. I think those questions contain their answers as well – when you need something duct-taped together, write a user defined control. If you need some robust piece of functionality – write a custom control. If you need just one more thing to add (a blinking tooltip) just extend the ToolTip class – these are pretty straightforward. Continue reading “Implement user-defined controls”

Create and Consume Custom Controls

ASP.NET ships with a lot of controls, but sometimes, they aren’t enough for specific needs. Luckily, the ASP.NET control model is easily extendible. You can create your own controls in two ways:

  1. Create a Custom Control
  2. Create a User Control.

In this post (and in the exam, hopefully) you will need to be familiar only with the latter. A User Control is much like a standard aspx page. You use existing controls to build up your user interface, then write the code behind classes.

However, there are some differences as well:

  • The file name extension is .ascx
  • There is no @Page directive, but a @Control
  • They cannot be run by themselves, only within an aspx page
  • Html, body, head and form tags are forbidden in User controls

You can add a User control to your page two ways: register it, or set a reference on it. It is a better approach to use the register method. If you just set a reference on a User control, you’ll only be able to dynamically add it in your code-behind file. But let’s see how to do it:
Continue reading “Create and Consume Custom Controls”