So in another blog, I mentioned a technique where I could hide obsolete content throughout my site by simply placing ContentPlaceHolders into hidden panels in the MasterPage. In that context, this practice might be a temporary fix pending a visit to each page in the web site.
Since our business involves a lot of mobility and we invite customers to visit our web site from their pocket pc, I’ve been thinking that this technique of "hiding" content placeholders might offer a better way to create a Pocket PC friendly user experience.
Currently, I substitute the CSS file or even redirect the user to a special section of my web site whenever I detect they are using Pocket IE. You can see this in action if you visit http://www.pocketforms.net (first in your browser then from your pocket pc.)
With Dynamic MasterPages, I can programmatically select a specific master page based on the requestor's browser type. The pocket pc (or smartphone) master page, for example, can reference a style sheet (.CSS file) which is appropriate for the device and also hide superfluous ContentPlaceHolders such as banners and advertising. This approach works much better than a pure CSS approach since hidden content is omitted server-side and never sent to the client.
The following code was inspired by Keith Smith's presentation at the Orlando .Net User Group and can be used to programmatically switch to the Pocket PC friendly master page when a "Windows CE" (pocket pc) browser is detected. In the following code, the master page, pocketpc.master, changes the layout, hides some of the content and references a CSS file with fonts and layout which are better suited for the smaller form factor of a pocket pc.
protected void Page PreInit(object sender, EventArgs e) {
if(Request.Browser.Browser.ToLower.Contains("windows ce")){
this.MasterPageFile = "~/pocketpc.master";
}
else {
this.MasterPageFile = "~/MasterPage.master";
}
}
In VB.Net, it looks like this:
Protected Sub Page PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
If Request.Browser.Browser.ToLower.Contains("windows ce") Then
Me.MasterPageFile = "~\pocketpc.master"
Else
Me.MasterPageFile = "~\MasterPage.master"
End If
End Sub
Notice that the opportunity to swap out the MasterPageFile occurs in the PreInit method which occurs early in the page lifecycle (before any custom controls are loaded.) This means that the code cannot be placed into a web custom control (which would be really convenient) but, instead must be placed into the inline code or code-behind file of each page.
Rather than adding this code to each page, this begs for a technique you always hear Carl Franklin, Richard Campbell and their guests talking about on .Net Rocks. That is, to have all your web forms inherit from a custom class rather than from the default, System.Web.UI.Page. When you add behaviors such as variable MasterPages to your custom page class, then all of your (derived) web forms will inherit that behavior as well. Since each of my pages inherit from a class called “webFormBase”, I simply added the code into the Page PreInit event within webFormBase and I get automatic master page switching.
Watch for another blog on this subject for a generic sample of this in action.