Programming Mobile Forms : Palindrome

Here is a simple Form which checks if the input text is a palindrome or not. This serves as a good introduction to the relatively new world of Programming for Mobile Web Forms using the Mobile Internet ToolKit. Our tools for this small experiment will be our familiar editor - Notepad or any other text editor. Alternatively we can also use the Microsoft Mobile Internet Designer in Microsoft Visual Studio.Net.

Step 1> Directives

Add the directives to the start of the Mobile Page-if you plan to program a lot for Mobile Web Forms, you can expect to see a lot of these lines (as a matter of fact - In every mobile page you create!)

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="cs" %>
<%@ Register TagPrefix="mobile"
="System.Web.Mobile" %>

Step 2> User Input

Now we add some lines for getting the user input (We have to care about what the user wants-right ?)

mobile:Form runat="server" ID="Form1">
<mobile:Label runat="server" ID="Label1">PALINDROME CHECK</mobile:Label>
<mobile:Label runat="server" ID="Label2">Enter the word:</mobile:Label>
<mobile:TextBox runat="server" id="Word" />
<mobile:Command runat="server" OnClick="cmdSubmit_OnClick" Text="Check!"
<mobile:Form runat="server" id="Result">
<mobile:Label runat="server" id="lblMessage" />

This simply adds labels, a text box and a button control to the Mobile page.

When the button is clicked, the function "cmdSubmit_OnClick" is invoked.

Step 3> Function

Let's check out the "juicy" details of cmdSubmit_OnClick-this function will be doing the core of the work for us.

We extract the word entered by the user in the mobile page, reverse it and compare the two; if the two are equal, then we have a palindrome on our hands!

The script evaluates the word and the results are displayed on the Results label on Form2. Note that Form2 is displayed by making it the ActiveForm.

<script runat="server" language="c#">
protected void cmdSubmit_OnClick(Object sender, EventArgs e)
char[] reverse = Word.Text.ToCharArray();
Array.Reverse (reverse);
String strreverse = new String(reverse);
if (strreverse != Word.Text)
lblMessage.Text = Word.Text + " is not a palindrome";
lblMessage.Text = Word.Text + " is a palindrome";
catch(Exception ex)
lblMessage.Text = "Error in operation. Please try again";
ActiveForm = Result;

Step 4> How Does It Look ?

You can verify the mobile page on your cell phone or an emulator or your friendly neighborhood Internet Explorer.


Figure 1: Input mobile page


Figure 2 : Output mobile page


Figure 3: Now we know that "palindrome" is not a palindrome.