TemplateContainer Class
Assembly: System.Web.Mobile (in system.web.mobile.dll)
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.
Note: |
|---|
| 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="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>
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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Web.UI.Control
System.Web.UI.MobileControls.MobileControl
System.Web.UI.MobileControls.Panel
System.Web.UI.MobileControls.TemplateContainer
System.Web.UI.MobileControls.MobileListItem
Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: