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="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>
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 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: