Using Device-Specific Filters


The mobile controls simplify the process of developing applications for disparate devices. They also eliminate the need to write several different kinds of markup to support the mobile user. However, often we do have the luxury of knowing that a large group of users has standardized on a single device or a class of similar devices. When trying to get the most out of adaptive renderings, it is nice to be able to exercise more fine-grained control over the output. The mobile controls support several different ways to customize based on the capabilities of a device by leveraging filters based on the capabilities of the requesting device. A filter is a named test used to modify control properties and affect rendering. Code Listing 4-8 is a sample web.config file that declares several filters for testing what type of markup is being used.

Code Listing 4-8: FiltersWeb.config

start example
 <configuration>
<system.web>
<deviceFilters>
<filter
name="prefersXHTML"
compare="PreferredRenderingType"
argument="xhtml-mp" />
<filter
name="prefersWML11"
compare="PreferredRenderingType"
argument="wml11" />
</deviceFilters>
</system.web>
</configuration>
end example

When these filters are used, the PreferredRenderingMime property of the Browser object is compared against the argument. The filter is true when there is a match. The filters provide a declarative way to reuse these capability tests in the mobile page. We include a deviceSpecific element inside a control and can override the control values based on the filters. The first filter that is true is applied and the remaining ones are not tested. Code Listing 4-9 demonstrates using these device filters to change the text of a label.

Code Listing 4-9: LabelFilter.aspx

start example
 <%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<script runat="server" language="C#">
public bool prefersHTML(System.Web.Mobile.MobileCapabilities
capabilities,string argument) {

if(capabilities["preferredRenderingType"] == "html32") {
return true;
}
return false;
}
</script>
<mobile:form runat="server">
<mobile:label runat="server" Text="The Default Text">
<deviceSpecific>
<choice filter="prefersXHTML" Text="XHTML Text" />
<choice filter="prefersHTML" Text="Text for HTML browser" />
<choice filter="prefersWML11" Text="WML" />
</deviceSpecific>
</mobile:label>
</mobile:form>
end example

Notice that we use a filter not included in Code Listing 4-8. If a filter is not found in the configuration, the mobile page looks for a method on the page to be used as a delegate in performing the filter check. When the page is requested, the preferredRenderingType is checked. If a filter is evaluated to true, the value of Text for the label is set to the new value and filter evaluation ends. If no match is found, the current value is kept.

Tip

Be careful when defining and using filters. There is a tendency to want to believe that one capability implies another. Even though the capabilities of new devices continue to advance rapidly, it is best to explicitly check for support when customizing.

In addition to being able to modify the properties and styles of controls using device filters, you can exert control over the markup itself. By using filters along with templates, you can easily customize the look of the page. The Panel control supports a template named contentTemplate that can be controlled with filters. The Form control supports several templates for customizing the rendering, including a scriptTemplate for passing through arbitrary script content to the browser as well as header and footer templates. Code Listing 4-10 demonstrates using a filter to use tables when they are supported.

Code Listing 4-10: HeaderFilter.aspx

start example
 <%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<script runat="server" language="C#">
public bool supportsTables(System.Web.Mobile.MobileCapabilities capabilities, string argument) {

if(Convert.ToBoolean(capabilities["tables"])) {
return true;
}
return false;
}
</script>
<mobile:form runat="server">
<deviceSpecific>

<choice filter="supportsTables">
<headerTemplate>
<table><tr><td><b>ASP.NET Coding Strategies</b></td></tr>
<tr><td>
</headerTemplate>

<footerTemplate>
</td></tr></table>
</footerTemplate>
</choice>

<choice>
<headerTemplate>
<mobile:label runat="server" Text="ASP.NET" />
</headerTemplate>
</choice>
</deviceSpecific>
</mobile:form>
end example

Notice in Code Listing 4-10 that we have included a choice element without a filter. When no filter is specified and none of the previous choice elements have been selected, that element automatically will be used.




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net