Writing Your Own Controls
The mobile controls architecture provides multiple extensibility options via user controls, inheritance controls, and composite controls. In all of these methods you're using existing controls and adding functionality to them. However sometimes none of the controls suffice, and creation of a new control from scratch is required.
When you write a custom control from scratch you need to do three things:
- Create a new mobile control in an assembly
- Write your own adapters for this control and
- Provide adapter mappings.
Further, there are three principles to keep in mind when writing your controls:
- Your control code should be device-independent
- You need to write specific adapters for each type of device that requires a different type of rendering for the control and
- Your code design should be reusable so that other developers can inherit properties and methods from your control.
You are going to create a new multi-line textbox control that isn't present in the ASP.NET mobile controls. Building this new control is a four-step process:
- Create a class library project for the control
- Inherit from the MobileControl base class
- Add the necessary methods, properties, and events
- Compile into an assembly
Step 1: Create a Class Library Project for the Control.
The first thing you need to do is create an ASP.NET Mobile Web project. It will create a mobile Web page that will be a test harness for the control. To do it, create a new Visual Studio.NET project; choose ASP.NET Mobile Web Application as the template type. Name the project as "ASP.NET Mobile Controls." Visual Studio.NET will create the basic project with a default mobile Web form called MobileWebForm1.aspx.
Now add a new project to this solution. Choose Class Library as the Template type and name the project "DirectMobileClassLibrary." Visual Studion.NET will create a new class file, which you should rename to MultiLineTextBox.vb. Rename the class name as well to MultiLineTextBox.
To the DirectMobileClassLibrary you need to add references to the System.Web and System.Web.Mobile components. Right click the project name in Solution Explorer, choose Add reference, and choose System.Web.dll and System.Web.Mobile.dll.
Add the following Imports statements at the top of the class file MultiLineTextBox.vb:
Step 2: Inheriting from the MobileControl Base Class
You need to modify the class definition to inherit from the MobileControl base class. The MobileControl class is the base class for all ASP.NET mobile controls; it contains style and context information common to all controls. You should also provide a namespace for this class as shown below.
Step 3: Add the Necessary Methods, Properties, and Events
Public Class MultiLineTextBox : Inherits MobileControl
For a multi-line text box you need three properties: rows, cols, and text. ViewState is used to store the property values. When using ViewState to store a value you must always check to make sure it's not null/nothing as otherwise a null exception will be raised (see Listing 1
Step 4: Compile into an assembly
Hit Ctl+Shift+F5. This will build the entire solution file and generate the control assembly.
Your control is now ready. Now you need to create the control adapter and then create the mapping between the control and the adapter.