Wednesday, April 16, 2008

Easy AJAX Steps for Success

Here are 10 helpful hints if you are considering using AJAX in an upcoming project:
  1. You don't need an AJAX library
  2. Download Aptana. Make it your default .js editor inside Visual Studio.
  3. Place all Javascript in external files (like codebehind)
  4. Instead of making invoking requests directly from your pages, build a class library that does the dirty work.
    hint: once you have these base libraries, add the files the code assist window to get auto-complete inside Aptana. Aptana lets you add documentation comments as well as parameter and return type information. Use it!
  5. You don't need an AJAX library.
  6. You can make classes in Javascript too.
  7. You don't have to define seperate functions for async callbacks from webservices. Use anonymous methods to inline your code in the calling method.
  8. Consider providing ways to extend standard HTML elements rather that trying to make up all sorts of funky controls. Controls have a place, but consider the common example of a grid. A grid is really just a table. Don't create a grid class, provide a way extend the table and add column sorts, row hover handling, row selection, etc. to it. Then, you can use standard HTML layouts and design and simply call a function or two to inject the features you want.
  9. Make sure to secure your AJAX server side.
  10. You don't need an AJAX library.

Why do I point out over and over that you don't need an AJAX library? Because all the AJAX hype out there makes the problems out to be a lot more difficult than they are. I would recommend one little script to abstract away SOAP or JSON requests, since you probably don't want to build that manually, but you don't really need much else to get started building AJAX into your applications. The most important thing to do is get a solid understanding of Javascript and CSS and make sure that you use standard DOM properties as much as possible. A great editor like Aptana can also be an extremely valuable tool. This isn't to say that using an AJAX library is a bad thing, just that you can get by fine without one if you would rather not add the extra dependancies and complexity to your app. The browser differences are not nearly as bad as the mobs might have you believe.

Also keep in mind that you don't have to make every element clickable and every node draggable. Web apps are not windows apps. You don't need one base page that everything is dynamically added to and removed from. On the contrary, it is nice to have back and forward buttons that actually do something. A lot of improvement can be made by simply adding a few AJAX callbacks to eliminate those annoying pages you used to have to make with 2 lines of text and a button.

Tuesday, April 15, 2008

Windows Mobile Tip: Accessing the Run dialog

Windows Mobile has had a run dialog similiar to the one found in desktop versions of Windows since Windows 95 for as long as I can remember. This feature is not well known however.

On the main Today screeen of a Windows Mobile powered Pocket PC (Windows Mobile Professional or Classic for those up with the current lingo) you can tap and hold down the ‘Action’ key while tapping and holding the stylus on the clock shown on the nav bar. The ‘Action’ key is usually the center of the D-Pad or Ctrl if you are using the emulator.

When you release the ‘Action’ key a small popup menu will appear, and you can select the ‘Run’ option which will display a small dialog.

Within this dialog you can enter the path to any executable, as well as any required command line parameters.

Examples you may like to try out are as follows:

  • calc.exe
  • iexplore.exe
  • ctlpnl.exe cplmain.cpl,4,0

For more details on accessing control panel applets from the command line, like the last example demonstrates refer to the Using Control Panel article on the Pocket PC Developer Network website.

This feature can be handy during development of applications for tasks such as re-registering COM dlls, however it is rather impractical for day to day use due to the typical slowness of entering the command line and the fact that unlike on the desktop there is no history of previous commands.

As an additional tip, did you know that if you tap and hold without the ‘Action’ key being pressed a different popup menu will appear? This one allowing you to select between an analogue and digital clock being displayed on the navbar.

Christopher Fairbairn

ListView columns reorderable

Another finishing touch that I like to see in applications that use ListViews is the ability for the end user to re-order the columns to suit their own preferences. This blog entry discusses one approach for adding this functionality to the ListView control present within the .NET Compact Framework.
Although it is difficult to convey in a static screenshot, the screenshot above shows a user dragging the stylus over the header of the listview control to move the position of the “First Name” column.

Obtaining Draggable Columns


The System.Windows.Forms.ListView control is a wrapper over top of the native ListView control. The native ListView control supports the notion of extended styles, which allow various optional features to be enabled or disabled as desired. One of the extended styles is called LVS_EX_HEADERDRAGDROP. If this extended style is enabled the user can re-order the columns by dragging and dropping the headers shown at the top of the listview while it is in report mode.

Although the .NET Compact Framework ListView control does not expose a mechanism to enable extended styles, we can use a technique discussed in a previous blog entry of mine to add or remove the LVS_EX_HEADERDRAGDROP extended style as desired.


private const int LVM_SETEXTENDEDLISTVIEWSTYLE = 0x1000 + 54;
private const int LVS_EX_HEADERDRAGDROP = 0x00000010;

public static void SetAllowDraggableColumns(this ListView lv, bool enabled)
{
// Add or remove the LVS_EX_HEADERDRAGDROP extended
// style based upon the state of the enabled parameter.
Message msg = new Message();
msg.HWnd = lv.Handle;
msg.Msg = LVM_SETEXTENDEDLISTVIEWSTYLE;
msg.WParam = (IntPtr)LVS_EX_HEADERDRAGDROP;
msg.LParam = enabled ? (IntPtr)LVS_EX_HEADERDRAGDROP : IntPtr.Zero;

// Send the message to the listview control
MessageWindow.SendMessage(ref msg);
}

This method allows the drag feature to be turned on and off for a given ListView control. Notice that this method makes use of a C# 3.0 feature called Extension Methods. The “this” keyword in front of the first parameter means that this method can be called as if it was part of the standard ListView control, meaning the following code snippet will work (assuming listView1 is an instance of the System.Windows.Forms.ListView control).

listView1.SetAllowDraggableColumns(true);

This is pure syntactic sugar, behind the scenes the C# compiler is simply passing in listView1 as the first parameter to the SetAllowDraggableColumns method.


Persisting Column Order Preferences

Once you have reorder-able columns it can be desirable to persist the user’s preferred layout across multiple executions of your application. It would be a pain if the columns always defaulted back to a standard order everytime the form was displayed.

The native ListView control provides two window messages, LVM_GETCOLUMNORDERARRAY and LVM_SETCOLUMNORDERARRAY that can be used to implement this feature. The code sample available for download wraps up these two window messages to allow you to query the current order of the columns by using a statement such as the following:

int[] columnOrder = listView1.GetColumnOrder();
// TODO: save 'columnOrder' to the registry
// or another persistent store

When columns are added to a ListView they are given an index. The first column is column 0 while the second is column 1 and so on. When columns are re-ordered they keep their index value but their position on screen changes. The array returned by the GetColumnOrder function contains the index for each column in the order that they are visible on screen. For example if the array contains the values 2, 0, and 1 it means that the last column (column 2) has been dragged from the right hand side of the listview to become the left most column.

Once we have obtained the order of the columns we can store the data in any persistent storage mechanism such as a file, a database table, or registry key. When the form is reloaded we can initialise the default order of the columns by calling the equivalent SetColumnOrder method with the value we previously saved:

// TODO: should read 'columnOrder' from the registry
// or other persistent store
int[] columnOrder = new int[]{2, 0, 1};

listView1.SetColumnOrder(columnOrder);
Christopher Fairbairn

Monday, April 14, 2008

Thousands of visitors daily - 10 things you have to do

Without talking to much, the success behind every great site, blog, social networks community is in it's users. Undeniable fact. In every piece of the website lot of effort, marketing, design, usability needs to be put in.

Here's a 10 things you have to do checklist:

1. Value exchange: If the product involves a registration process (linkedin, facebook, etc) make sure that the process is as simple as possible or ensure a “value exchange” to give the user an incentive to go through the registration process.

2. Niche and branch out: Sometimes it makes sense to focus your first release on niche segments instead of a big bang mass market approach.

3. Let them evangelize: If your product has a premium paid service – make sure there is enough to wow the customers in the free version. Build your product in such a way that the customers can evangelize it.

4. Distributables: Build useful “distributables” that can link back to your site. Make sure that these are simple to distribute online.


5. Downloadables: It’s debatable on how effective downloadable desktop clients are. Some products are web only; some can be download-and-use or can be a hybrid of both. You need to have the right balance for your product. Smilebox’s Yannis said that about 50% of the users dropped out when they encounter a downloadable executable in the process.


6. First 100 users: Once you have built, hopefully, what is a compelling product; evangelize it with your friends and family. This includes relatives, common friends, posts in your alumni networks etc. This should help the user base reach the 100s.

7. The next 1000:The next level of growth can be from bloggers, SEO and SEM - follow some of the prominent bloggers in your area (including techcrunch) and build relationships with them by having genuine interest in what’s covered in the blog, contributing to it (comments,etc).

A positive mention in the blogs could lead to a considerable increase in traffic. No one discounts the value of SEO and SEM. Some free resources on SEO can be found at (www.seomoz.com). But as a general rule make it easy for users to create incoming links to your site. There is a temptation to create a great “look and feel” by having a lot of flash but remember that this prevents the discovery by search engines, so use them judiciously.

8. Personalization and game behavior: Personalization can be a great asset in retaining users.”My Yahoo” was a great success in spite of a huge proliferation of news sources/aggregators because the user spent more than 3 minutes in configuring the settings that suits him/her. Having done this the user has a great affinity to coming back to what was his/her setup/creation.

Also incorporating competition or game behavior in mundane activities can be a great source of engagement.

9. Market place networks: These are networks/communities that have two user groups with different goals -buyers/sellers, contributors/readers, teachers/students, job-hunters/recruiters, etc- it’s important to move two customer acquisition needles and treat both of them differently, with equal priority as both are equally important to the success of your online network.

10. Acquisition cost: If you are a pure free service (i.e. intent to make zero money from the user/consumer) the cost of acquiring the consumers should be as less as possible (close to zero).

Windows Vista - Boot faster

First thing you should do is always get rid of your temporary files and any unused programs. Once you have done that then run the defrag tool to reorganize your hard drive. Defragmenting your files puts them back into one contiguous space on your drive optimizing your boot time.

To get Vista's defrag tool to give you some information about your hard disk, and to control which hard disk or partition it defragments, you will need to use the command line defragmentation utility. It will still not give you any feedback while defragmenting, just as the GUI version of the defragmenter will not, but at least you can get information on the fragmentation level of your hard disk, control whether to defragment even if the file fragments are larger than 64 MB, and control which partition or hard disk to defragment.

To use the command line defrag tool in Windows Vista, you have to run the Command Prompt as an administrator. In Vista, this is not automatic even if you are logged in as the administrator. Click the Windows button (previously the Start button in earlier versions of Windows), the All Programs menu item and the Accessories menu item. Right click the "Command Prompt" button and select "Run as administrator". A command prompt window will appear. Everything you run in this Window will be run with administrator rights.

To view a file fragmentation analysis of (say) your C: drive, type:

defrag c: -a -v

The "-a" parameter tells the defragger to perform a fragmentation analysis. The "-v" option tells it to be verbose in its report. If you want a report on drive D: or some other drive, substitute that drive letter in place of c:.

To defragment a particular drive, say C:, type:

  1. defrag c: -v -r

    The "-r" option tells the defragmentation utility to treat files that are fragmented with 64 MB fragments or larger as though they are not fragmented. This partial defragmentation is the default for "defrag", and it's the only way the GUI defragmenter in Vista works.

    You can also force the defragmenter to defragment everything. That is, even if the file fragments are larger than 64MB, the Vista defragmenter will still attempt to put the file into contiguous sectors. To do this, run the defragger with the following options:

    2. defrag c: -v -w

    As you have probably have guessed, "-w" tells the Vista defrag tool to do a full defragmentation. All file fragments will be consolidated where possible.

    You will still not get any feedback as to the progress of the defragmentation with the command line tool, just as you did not with the GUI version. However, at the beginning and the end of the defragmentation, "defrag" with the "-v" option will give a report, much like the old Windows XP GUI defragmentation utility. Again, though, it will not report fragmented files with 64 MB fragments (or larger) as being fragmented.

Please NOTE: With the command line method there is no progress bar so you just have to sit and wait for it to finish.

If defragging doesn’t really help your boot performance then you should at least see improvement in your application load times when the operating system is finally booted up.

Also there is one quick edit to the registry that can make Vista shut down quicker than you can say “shut down.” Those not comfortable with delving in to the registry had better not proceed because making a mistake here could (and usually does) mess up your system bad.

At the Start Menu, type “regedit” without the quotes in the search box and hit Enter. Find HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control and go to the Control folder, right click the entry “WaitToKillServiceTimeout” and set the value to something lower, 1000 is usually good (the numbers represent milliseconds). The default value is a (too) generous 20000. However, the cost with this is that it won’t give running programs much time to save data so losing work using the tweak is a definite possibility.

iPhone kicking Nokia bum in U.K.

iPhone is the number two Internet mobile browser in the UK and number one in the US, according to StatCounter, operator of a service that provides website visitor tracking statistics..

StatCounter says that iPhone took 0.06 percent of the total Internet browser market in the UK in March, behind Nokia with 0.15 percent. "iPhone already has three times the Internet browser market share of BlackBerry (0.02 percent) in the UK [and] Sony Ericsson has 0.01 percent of the UK market...iPhone and iTouch combined took 0.9 percent of the UK market in March to date," StatCounter said.

Globally the StatCounter analysis shows that Nokia is still well ahead in the mobile Internet browser market with 0.25 percent in March to date. iPhone is number two with 0.06 percent (0.08 percent if iTouch is included), ahead of BlackBerry with 0.01 percent.

The StatCounter analysis finds that Microsoft Internet Explorer still leads the browser market overall with 53 percent in the UK. But it is facing real competition from Firefox which has 35 percent of the UK market. In the US the battle is neck and neck with Internet Explorer holding 44.5 percent and Firefox 43.9 percent in March.

StatCounter says its analysis was based on a sample of 371 million page views globally from December 07 to March 08, including 71 million page views in the US and 14 million in the UK.

OneNote - Sharing documents

There used to be a problem here at work. We didn't managed to share documents properly. We tried lots of methods, till we finally checked OneNote. You can easily tag information, and search for this quickly, and can even insert printouts into notebooks.

I was thinking that being in a company, the sharing documents feature is a request so i took a look over the capabilities of that software. Here are the simplest 3 ways of sharing documents between all:

A. You can create a OneNote notebook that’s enabled for sharing. You can then put this notebook on a Windows share on your network. Then you can open this notebook from the share on multiple machines, and each OneNote instance on a machine will figure out how to sync with this version on the network share. Each OneNote instance creates a local copy of the notebook and periodically syncs it’s local copy with the network copy. It’s constantly doing a diff, to figure out what change, and then committing those changes. It’s super easy to setup, and it works great!

B. You can tell OneNote to create a new shared notebook on your machine. OneNote will then create the notebook, and then create a network share automatically for that notebook and expose it to the network! Very easy too. The difference between this approach and the first is that in this case, the network share is on your machine, and in #1 the share is on a different machine.

C. The last approach is very cool as well. You can start a Live Sharing session in OneNote, and allow other OneNote users on the network interactively edit your notebook with you! They don’t have to have a copy of the notebook on their machines either! You just start a Live Sharing session and OneNote takes care of the rest… it starts a sharing server on a port, and opens a hole in the firewall. Then on the other machine, you simply connect to a live sharing session on the first machine (by providing it’s WINS name or IP address) and you are good to go! It works brilliantly! When you are done with your sharing session, a local copy is left on the remote machine, which you can work with locally! You can reconnect to the live sharing session later, etc. This is brilliant! What a great use of P2P technology! I’m surprised Microsoft doesn’t advertise this feature more.

OneNote is very helpfully if you want to manage lots of fluid information that must be persisted and replicated reliably, without having to copy files, and run cron jobs, or know how to use SVN and setup a SVN server.