共用方式為


Device Template Support 

Device template support is provided as part of the DeviceSpecific/Choice construct. Writing ASP.NET mobile controls that provide device templates is very similar to writing templated ASP.NET server controls. However, keep in mind the following differences that are unique to mobile controls:

  • Every mobile control that supports device templates implements the ITemplateable interface. This is a marker interface and has no methods.

  • Controls that support device templates must have a default rendering mode so that the control can still render on devices when no device templates are defined or specified.

  • Control developers need to decide how to treat device templates during rendering. One behavior is to switch to an exclusively templated mode if any device template is defined and chosen. The control then performs all of its rendering using provided templates. The List control is an example of such a control. Alternatively, a control can use templates in a purely additive process: If a certain template is defined and chosen, it can be used to add to or replace part of the default rendering. The Form control is an example of a control that exhibits this behavior.

  • Instead of exposing ITemplate properties, a mobile control has access to a collection of defined templates. The MobileControl base class has two members that can help a control use device templates:

    • The IsTemplated property indicates whether the control has any templates defined and chosen, as shown in the following code example.

      if (mobileControl.IsTemplated)
      {
          return true;
      }
      
    • The GetTemplate method returns the device template of the given name, or null if none is defined. The following example shows how device templates are retrieved and tested for null status.

      public override void CreateDefaultTemplatedUI(bool doDataBind) 
          {
              ITemplate headerTemplate = GetTemplate(Constants.HeaderTemplateTag);
              ITemplate footerTemplate = GetTemplate(Constants.FooterTemplateTag);
              ITemplate scriptTemplate = GetTemplate(Constants.ScriptTemplateTag);
              if (headerTemplate != null)
              {
                  _headerContainer = new TemplateContainer();
                  headerTemplate.InstantiateIn(_headerContainer);
                  _headerContainer.EnablePagination = false;
                  Controls.AddAt(0, _headerContainer);
              }
              if (footerTemplate != null)
              {
                  _footerContainer = new TemplateContainer();
                  footerTemplate.InstantiateIn(_footerContainer);
                  _footerContainer.EnablePagination = false;
                  Controls.Add(_footerContainer);
              }
              if (scriptTemplate != null)
              {
                  _scriptContainer = new TemplateContainer();
                  scriptTemplate.InstantiateIn(_scriptContainer);
                  _scriptContainer.EnablePagination = false;
              }
          }
      

The following table describes the templates that are available through the Form control. The Form control supports both device-specific and device-independent template sets.

Template Description

Header Template

The header template is rendered at the top of the form. If the form is paginated into several screens, each screen contains the header. The header template can be part of a device-independent template set. If the template set is device-independent, the header template contains mobile controls.

Footer Template

The footer template is rendered at the bottom of the form. If the form is paginated into several screens, each screen contains the footer. The footer template can be part of a device-independent template set. If the template set is device-independent, the footer template contains mobile controls.

Script Template

The script template is rendered at the top of the form. If the form is paginated into several screens, each screen contains the script template. The code within the script template is added directly after the opening <head> tag on HTML devices, and directly after the opening <card> tag on WML devices.

These templates are also available programmatically. For more information, see the Footer property, Header property, and Script property.

See Also

Concepts

Template Sets and Templated Controls
Implementing Templated Rendering

Other Resources

Adding New Device Adapters and Device Support