Extending Mobile Controls

   

As with Web controls, much of the power of the mobile controls is in the capability to extend them. Extended or composite controls enable you to minimize the effort needed to reuse code, and make it easy to standardize the layout and handling of common tasks . You can create or extend mobile controls in any of the ways that you did with Web controls.

User Controls

Making a user control is the easiest way to group mobile controls into a reusable object. User controls differ from composite controls in that the control persists as an .ascx file. A composite control is compiled and persists as a .dll file.

User controls are defined identically to mobile pages with the following exceptions:

  • Do not require a page directive

  • Do not have to reside inside <Form></Form> tags

  • Must have a file extension of .ascx

  • Must contain the @Register directive common to all mobile Web forms pages

The Hello World example can show how user controls are used. Listing 20.9 shows this with the label as a separate control.

Listing 20.9 User Control ASP.NET Code
 <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls"  Assembly="System.Web.Mobile" %>  <mobile:label id="Label1" runat="server">          Hello, world!  </mobile:label> 

To use the user control, you must first register it with the @Register directive at the beginning of your .aspx file. After the control is registered, it can be used like any other control. Listing 20.10 shows how the Hello World user control is registered and used.

Listing 20.10 Using the User Control
 <%@ Register TagPrefix="My" TagName="HelloLabel" Src="Hello.ascx" %>  <%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls"  Assembly="System.Web.Mobile"%>  <%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="CS" %>  <mobile:form id="Form1" runat="server">      <My:HelloLabel id="hello" runat="server" />  </mobile:form> 

The compiler will insert the text of the .ascx file where the control is implemented when the .aspx file is compiled. Listing 20.10 outputs Hello World using the user control that was shown in Listing 20.9.

Composite Controls

Mobile composite controls are implemented the same way as composite controls for Web Forms, which were discussed in Chapter 13, "Writing Controls for ASP.NET." The biggest difference is that you should derive your control from System.Web.UI.MobileControls.MobileControl, rather than from System.Web.UI.WebControls.WebControl. Listing 20.11 shows the user control for the Hello World page re-implemented as a composite control.

Listing 20.11 C# Code for Composite Control
 namespace My.MobileControls  {      class HelloCtl : MobileControl      {          // Create child controls by overriding the CreateChildControls          // method.          protected override void CreateChildControls()          {              Label label;              label = new Label();              label.Text = "Hello World";              Controls.Add(label);              label = new Label();               label.Text = "Hello World";              Controls.Add(label);          }      }  } 

Listing 20.12 shows the ASPX code needed to register and use the preceding control.

Listing 20.12 ASPX Code to Use the Hello World Control
 <%@ Register TagPrefix="My" TagName="HelloCtrl"      Namespace="My.MobileControls" %>  <%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls"      Assembly="System.Web.Mobile"%>  <%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="CS" %>  <mobile:form id="Form1" runat="server">      <My:HelloCtrl id="hello" runat="server" />  </mobile:form> 

This composite control outputs Hello World twice.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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