Export (0) Print
Expand All

TemplateContainer Class

Derived from the Panel class and used by ASP.NET to contain DeviceSpecificChoice templates.

Namespace: System.Web.UI.MobileControls
Assembly: System.Web.Mobile (in system.web.mobile.dll)

public class TemplateContainer : Panel, INamingContainer
public class TemplateContainer extends Panel implements INamingContainer
public class TemplateContainer extends Panel implements INamingContainer

If a mobile control provides templated rendering features through device templates, it must create instances of each template inside a separate control of type TemplateContainer. If a data-binding expression is in a control in a template, the Container variable of the expression is always of type TemplateContainer.

The following code example demonstrates how to use TemplateContainer objects to create templates within a DeviceSpecific object in a mobile form.

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

<%@ Page Language="C#" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" 
    Assembly="System.Web.Mobile" %>

<script runat="server">
    private void Form_Init(object sender, System.EventArgs e)
    {
        // Create a DeviceSpecific group for Choice elements
        DeviceSpecific devSpecific = new DeviceSpecific();

        // Create two Choice objects, one with a filter
        for (int i = 0; i < 2; i++)
        {
            DeviceSpecificChoice devChoice;
            ITemplate custTemplate;

            // Create a Choice object 
            devChoice = new DeviceSpecificChoice();
            // Only the first Choice has a filter (must be in Web.config)
            if (i == 0)
                devChoice.Filter = "isHTML32";

            // Create the header template.
            custTemplate = new CustomTemplate("HeaderTemplate");
            // Put header template in a new container
            custTemplate.InstantiateIn(new TemplateContainer());
            // Add the header template to the Choice
            devChoice.Templates.Add("HeaderTemplate", custTemplate);

            // Create the footer template
            custTemplate = new CustomTemplate("FooterTemplate");
            // Put footer template in a new container
            custTemplate.InstantiateIn(new TemplateContainer());
            // Add the footer template to the Choice
            devChoice.Templates.Add("FooterTemplate", custTemplate);

            // Add the Choice to the DeviceSpecific
            ((IParserAccessor)devSpecific).AddParsedSubObject(devChoice);
        }

        // Add the DeviceSpecific object to the form
        ((IParserAccessor)Form1).AddParsedSubObject(devSpecific);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.UI.MobileControls.Label lab;
        lab = (System.Web.UI.MobileControls.Label)Form1.Header.FindControl("Label1");
        if (lab == null)
            return;

        // Get the selected choice's filter name
        string filterName = 
            Form1.DeviceSpecific.SelectedChoice.Filter;
        if (filterName == "isHTML32")
            lab.Text += " - HTML32";
        else
            lab.Text += " - Default";
    }

    public class CustomTemplate : ITemplate
    {
        String templateName;

        // Constructor
        public CustomTemplate(string TemplateName)
        {
            templateName = TemplateName;
        }

        public void InstantiateIn(Control container)
        {
            if (templateName == "HeaderTemplate")
            {
                // Create a label
                System.Web.UI.MobileControls.Label lab;
                lab = new System.Web.UI.MobileControls.Label();
                lab.Text = "Header Template";
                lab.ID = "Label1";

                // Create a command
                Command cmd = new Command();
                cmd.Text = "Submit";

                // Add controls to the container
                container.Controls.Add(lab);
                container.Controls.Add(cmd);
            }
            else if (templateName == "FooterTemplate")
            {
                // Create a label
                System.Web.UI.MobileControls.Label lab;
                lab = new System.Web.UI.MobileControls.Label();
                lab.ID = "Label2";
                lab.Text = "Footer Template";

                // Add label to the container
                container.Controls.Add(lab);
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:form id="Form1" runat="server" OnInit="Form_Init">
    </mobile:form>
</body>
</html>

All of the code above can be replaced declaratively with the following markup:

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:Form id="Form1" runat="server">
        <mobile:DeviceSpecific Runat="server">
            <Choice Filter="isHTML32">
                <HeaderTemplate>
                    <mobile:Label ID="Label1" Runat="server">
                        Header Template - HTML32</mobile:Label>
                    <mobile:Command Runat="server">
                        Submit</mobile:Command>
                </HeaderTemplate>
                <FooterTemplate>
                    <mobile:Label ID="Label2" Runat="server">
                        Footer Template</mobile:Label>
                </FooterTemplate>
            </Choice>
            <Choice>
                <HeaderTemplate>
                    <mobile:Label ID="Label1" Runat="server">
                        Header Template - Default</mobile:Label>
                    <mobile:Command ID="Command1" Runat="server">
                        Submit</mobile:Command>
                </HeaderTemplate>
                <FooterTemplate>
                    <mobile:Label ID="Label2" Runat="server">
                        Footer Template</mobile:Label>
                </FooterTemplate>
            </Choice>
        </mobile:DeviceSpecific>
    </mobile:Form>
</body>
</html>

The Web.config file must have the following lines:

<!-- In Web.config file -->
<deviceFilters>
  <filter name="isHTML32" compare="PreferredRenderingType" argument="html32" />
</deviceFilters>

System.Object
   System.Web.UI.Control
     System.Web.UI.MobileControls.MobileControl
       System.Web.UI.MobileControls.Panel
        System.Web.UI.MobileControls.TemplateContainer
           System.Web.UI.MobileControls.MobileListItem

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft