共用方式為


TemplateContainer 類別

衍生自 Panel 類別並由 ASP.NET 用於包含 DeviceSpecificChoice 樣板。

命名空間: System.Web.UI.MobileControls
組件: System.Web.Mobile (在 system.web.mobile.dll 中)

語法

'宣告
Public Class TemplateContainer
    Inherits Panel
    Implements INamingContainer
'用途
Dim instance As TemplateContainer
public class TemplateContainer : Panel, INamingContainer
public ref class TemplateContainer : public Panel, INamingContainer
public class TemplateContainer extends Panel implements INamingContainer
public class TemplateContainer extends Panel implements INamingContainer

備註

若行動控制項透過裝置樣板來提供樣板化呈現功能,則必須在型別為 TemplateContainer 的各個控制項中,建立每個樣板的執行個體。如果資料繫結 (Data Binding) 運算式位於樣板的控制項中,運算式 Container 變數的型別一定是 TemplateContainer

範例

在下列程式碼中,示範了如何使用 TemplateContainer 物件在行動表單的 DeviceSpecific 物件內建立樣板。

Note注意事項

以下程式碼範例使用單一檔案程式碼模型,如果直接複製到程式碼後置 (Code-Behind) 檔案即可能無法作用。這個程式碼範例必須複製到副檔名為 .aspx 的空白文字檔。如需 Web Form 程式碼模型的詳細資訊,請參閱 ASP.NET Web 網頁程式碼模型

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

<script runat="server">
    Private Sub Form_Init(ByVal sender As Object, ByVal e As EventArgs)
        ' Create a DeviceSpecific group for Choice elements
        Dim devSpecific As New DeviceSpecific()
        Dim ipa As IParserAccessor

        ' Create two Choice objects, one with a filter
        For i As Integer = 0 To 1
            Dim devChoice As DeviceSpecificChoice
            Dim custTemplate As ITemplate

            ' Create a Choice object 
            devChoice = New DeviceSpecificChoice()
            ' Only the first Choice has a filter (must be in Web.config)
            If i = 0 Then
                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)
            End If
            
            ' Add the Choice to the DeviceSpecific
            ipa = CType(devSpecific, IParserAccessor)
            ipa.AddParsedSubObject(devChoice)
        Next

        ' Add the DeviceSpecific object to the form
        ipa = CType(Form1, IParserAccessor)
        ipa.AddParsedSubObject(devSpecific)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim lab As System.Web.UI.MobileControls.Label
        lab = CType(Form1.Header.FindControl("Label1"), _
            System.Web.UI.MobileControls.Label)
        If IsNothing(lab) Then Return

        ' Get the selected choice's filter name
        Dim filterName As String = _
        Form1.DeviceSpecific.SelectedChoice.Filter
        If filterName = "isHTML32" Then
            lab.Text += " - HTML32"
        Else
            lab.Text += " - Default"
        End If
    End Sub

    Public Class CustomTemplate
        Implements ITemplate
        Dim templName As String

        ' Constructor
        Public Sub New(ByVal TemplateName As String)
            templName = TemplateName
        End Sub

        Public Sub InstantiateIn(ByVal container As Control) _
            Implements ITemplate.InstantiateIn
            
            If templName = "HeaderTemplate" Then
                ' Create a label
                Dim lab As New System.Web.UI.MobileControls.Label
                lab.Text = "Header Template"
                lab.ID = "Label1"

                ' Create a command
                Dim cmd As New Command()
                cmd.Text = "Submit"

                ' Add controls to the container
                container.Controls.Add(lab)
                container.Controls.Add(cmd)
            
            ElseIf templName = "FooterTemplate" Then
                ' Create a label
                Dim lab As System.Web.UI.MobileControls.Label
                lab = New System.Web.UI.MobileControls.Label()
                lab.ID = "Label2"
                lab.Text = "Footer Template"

                ' Add label to the container
                container.Controls.Add(lab)
            End If
        End Sub
    End Class

</script>

<html xmlns="http:'www.w3.org/1999/xhtml" >
<body>
    <mobile:form id="Form1" runat="server" 
        OnInit="Form_Init">
    </mobile:form>
</body>
</html>
<%@ 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>

所有上述程式碼都可以使用下列標記以宣告方式取代:

<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>

Web.config 檔必須含有下列幾行:

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

.NET Framework 安全性

繼承階層架構

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

執行緒安全

這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。

平台

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

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

版本資訊

.NET Framework

支援版本:2.0、1.1

請參閱

參考

TemplateContainer 成員
System.Web.UI.MobileControls 命名空間
MobileListItem 類別

其他資源

Understanding ASP.NET Adaptive Rendering
容器控制項
開發 Mobile Web 應用程式
行動裝置能力