DeviceSpecific Class
Assembly: System.Web.Mobile (in system.web.mobile.dll)
Within a <DeviceSpecific> element, you typically specify one or more <Choice> elements, each containing attributes that specify how to evaluate the choice against target device capabilities. At run time, each choice is evaluated in order, and the first choice that is successfully evaluated is used. The DeviceSpecific/Choice construct is used to specify template sets and override properties; for example, it can be used to specify device-specific images for the Image control.
Note |
|---|
| Even though the DeviceSpecific class inherits from the Web Forms System.Web.UI.Control namespace, this is only an implementation detail. A <Choice> element does not behave like a control. |
The following code example demonstrates how to use DeviceSpecific and DeviceSpecificChoice objects to create interfaces specific to a variety of devices 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, 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>
For these examples, the Web.config file must have the following elements:
<!-- 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.
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.
Note