DeviceSpecific Class

Provides a construct for specifying a choice among multiple content alternatives in the <DeviceSpecific> element.

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

[PersistNameAttribute("DeviceSpecific")]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class DeviceSpecific : Control
<mobile:DeviceSpecific />

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.

NoteNote:

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.

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

System.Object
  System.Web.UI.Control
    System.Web.UI.MobileControls.DeviceSpecific

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft