Create a Mobile Friendly Website with ASP.NET Themes

by Justin 8. November 2008 10:15

Download the Demo (ASP.NET 2.0)

If you're reading this you realize the value of optimizing websites for mobile devices. I created an ASP.NET website that chooses a different theme depending on whether or not the device accessing the page is a mobile device. If a mobile device is recognized the theme displays a red background, otherwise a different theme is chosen which displays a blue background. Feel free to check out my online demo for custom theming mobile devices, and download a zipped version of the demo to see exactly how it works. Be sure to view the demo in both a mobile and non-mobile browser to get the full effect. If you don't have a mobile device try using the online Opera Mini simulator.

Supported Mobile Devices:

  • Apple iPhone
  • Apple iPod
  • Google Android
  • Opera Mini
  • PlayStation Portable
  • Handspring Blazer
  • Motorola Internet Browser
  • Pocket IE
  • RIM Browser
  • Nokia
  • Compact NetFront

How it Works

I've added some custom browser definitions to the App_Browsers folder. This extends the default ASP.NET browser definitions, which are not very good for recognizing mobile devices. There are two .browser files, courtesy of Owen Bradly's website, that contains the definitions. (I'll note that I added a definition for recognizing the Google Android as it was not in Owen's files.) Each time the Default.aspx page is request I check to see if a supported Mobile device is requested the page. If so then I change the theme to the mobile theme. The code to do this is very simple.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.Browser.IsMobileDevice)
            Theme = "Mobile";
    }
}

If you're using Master pages you can always set this in the master page's code behind instead of each individual page's code behind.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

asp.net

Using the HTML label element with ASP.NET

by Justin 1. May 2008 00:28

You've probably noticed that a <asp:Label /> element actually renders a <span> tag, not the <label for="name"> tag that's been around since HTML 4. Luckily, it's pretty easy to include the label element in your ASP.NET forms.

ASP.NET 2.0, 3.0, 3.5 and Later

To make the <asp:Label /> control render as a <label> instead of a <span> include the AssociatedControlID attribute like so.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox1" Text="Label"></asp:Label>
		<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
	</form>
</body>
</html>

 

ASP.NET 1.0, 1.1

Unfortunately, older versions of ASP.NET don't support the AssociatedControlID attribute. Instead, you'll have to create a regular HTML <label> element and manually write out the ID of the associated control in the "for" attribute. 

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
	<title>WebForm1</title>
	<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
	<meta name="CODE_LANGUAGE" Content="C#">
	<meta name="vs_defaultClientScript" content="JavaScript">
	<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
	<form id="Form1" method="post" runat="server">
		<label for="<%=TextBox1.ClientID %>">Label</label>
		<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
	</form>
</body>
</HTML>

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Convert that DetailsView's LinkButton to a Button

by Justin 7. February 2008 09:52
DetailsView with LinkButtons DetailsView with Buttons

I love .NET's Data Controls. Version 2.0 ushered in several new ones, one of which was the DetailsView. It's great except for one thing, the default buttons for "update", "insert", and "cancel" are LinkButtons! I first noticed this when I was working with the ASP.NET BeerHouse Starter Kit.

I've observed ordinary users trying to use these buttons and it always confuses them. The fact is, clicking text to submit a form is pretty rare, especially if the form is large and requires a lot of information. Almost always a button or a graphic that looks like a button is used. I'm a fan of consistency and intuitive user interfaces, so naturally I wanted to change the LinkButtons to good old-fashioned Buttons.

The problem is related to the AutoGenerateXXXXXButton attributes of the <asp:DetailsView /> control, where XXXXX is "Edit," "Insert," or "Delete." Get rid of them if you're using them. If you don't you'll find that you have two command fields later.

Create an <asp:CommandField /> element under the <Fields> tag of the DetailsView. Set the ButtonType to "Button." That is the most important part. If you don't set it it will default to "Link" and display LinkButtons again. We'll also need to tell it which command buttons to display via the ShowXXXXXButton attribute of the CommandField.  That's pretty much it. Below is an example of the code that generated the second graphic here on the right. The first graphic was the default look-and-feel for the DetailsView.

<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False"
    DefaultMode="Edit" DataSourceID="XmlDataSource1" GridLines="none">
    <Fields>
        <asp:BoundField DataField="Vin" HeaderText="Vin" SortExpression="Vin" />
        <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
        <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
        <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        <asp:CommandField ButtonType="Button" ShowCancelButton="true" ShowDeleteButton="true" 
            ShowEditButton="true" ShowInsertButton="true" />
    </Fields>
</asp:DetailsView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/CarList.xml"></asp:XmlDataSource>

 

If you'd rather use an image instead of a Button set ButtonType to "Image." You'll also have to give the URL of the image via the XXXXXImageUrl attribute of the CommandField, where "XXXXX" is the command type. (i.e. Cancel, Update, Insert, etc.)

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Justin HoltonHi, my  name is Justin. I cut my teeth learning HTML back when Netscape Navigator was still the most popular web browser. Later that inspired me to major in Computer Science at college. Today I'm a professional web developer with experience in everything from social networking application design to Search Engine Optimization (SEO). I believe the Internet is the most important achievement of man since the printing press, and I'm grateful that I was born in time to see it go from obscurity to a ubiquity.

Page List