My blog has moved! It can now be found at http://www.strongasanox.co.uk/


UPDATE: I have moved my blog to my own domain and this post now be found here.

The project I am currently involved with at work has several paging widgets i.e. for image slideshows, search results, etc. that contain summary information such as “Showing 1/8″.

So how can this information be marked up in HTML in an accessible manner?

Well, I cannot remember where I got the inspiration for this from – if I remember I will post an update – but here is the HTML that I decided to use:

<span class="summary">
    Showing 1<abbr title="out of a total of">/</abbr>8
</span>

Pretty nifty eh?

Please let me know what you think.


UPDATE: I have moved my blog to my own domain and this post now be found here.

If you ever find yourself needing to obtain the site root of your ASP.NET application you will hopefully find the following code useful:

public string SiteRoot {
    get {
	HttpRequest Request = HttpContext.Current.Request;
        string appPath = Request.ApplicationPath;
	if (appPath.Length > 1) {
            appPath += "/";
        }

	string portNum = string.Empty;
	if(Request.Url.Port != 80) {
	    portNum = string.Format(":{0}", Request.Url.Port);
	}

	return Request.Url.Scheme +
	    "://" +
            Request.Url.Host +
            portNum +
            appPath;
    }
}

On my last project it proved particularly useful as we could test it on our internal development servers before sending it across to the client; they themselves then ran the site on testing servers before deploying it onto their production server. All this was done without having to modify a single line of code or any configuration files.

If you are using ASP.NET 3.5 you could perhaps take this a stage further and implement this code as an extension method on, for example, the HttpRequest object.

If you have any suggestions on how this can be improved please let me know.


UPDATE: I have moved my blog to my own domain and this post now be found here.

I had been having trouble with my phone all day today.  For some unknown reason every time I tried to make a call or send a text message I was politely informed that there was no network coverage.  After trying some phone-arm-gymnastics to see if I could get a better reception 3 feet up and to the left a bit, a sudden flash of inspiration struck me: reboot my phone.

Now, in a previous job of mine, every time you phoned IT support the first thing they would ask is: “Have you tried rebooting?”.  So I turned my phone off and turned it back on and, lo and behold, everything was up and running again.

Just wished I tried it several hours earlier.


UPDATE: I have moved my blog to my own domain and this post now be found here.

YAGNI – short for “You Aren’t Going to Need It” – is the acronym given to not writing code until you are sure you are actually going to need it. While reading Google’s paper on BigTable the other day it was interesting to read that this was one of the lessons learned during its development:

Another lesson we learned is that it is important to delay adding new features until it is clear how the new features will be used. For example, we initially planned to support general-purpose transactions in our API. Because we did not have an immediate use for them, however,
we did not implement them.


UPDATE: I have moved my blog to my own domain and this post now be found here.

I have released the first version of my jQuery Form Focus plugin. This lets you set the initial focus on any form element but is careful not to set the focus if the user has already started filling in the form. To use it you call something like:

$('#username').formFocus();
$('form input:first').formFocus();
$('form#options input[type="checkbox"]:first').formFocus();

Hope you find it useful.


UPDATE: I have moved my blog to my own domain and this post now be found here.

The Web.sitemap file was introduced in ASP.NET 2.0 and provides a really easy way to bind data to menus in your site. For example, given a Web.sitemap file containing this:

<sitemap
    xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx" title="Home"
        description="">
<sitemapnode url="about.aspx" title="About"
        description="">
<sitemapnode url="contact.aspx" title="Contact"
        description="">
</sitemapnode>
</sitemapnode>

We can bind this to a Repeater control using a SiteMapDataSource control to generate a list of links like so:

<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
        ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
        DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>"><%# Eval("title") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>

This, unsurprisingly, renders the following HTML:

<ul>
<li>
<a href="about.aspx">About</a>
</li>
<li>
<a href="contact.aspx">Contact</a>
</li>
</ul>

Nothing you probably didn’t know already. But to make our menu more accessible, we can utilise the empty description attribute that Visual Studio provides us with by default to add some text to render as a HTML title attribute on each link:

<sitemap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx"
        title="Home" description="Return to homepage">
<sitemapnode url="about.aspx"
        title="About" description="About company XYZ">
<sitemapnode url="contact.aspx" title="Contact"
        description="How to get in touch with us">
</sitemapnode>
</sitemapnode>

Now we just need to tweak our code slightly:

<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
    ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
        DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>" title="<%# Eval("description") %>">
<%# Eval("title") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>

Which will render:

<ul>
<li>
<a title="About company XYZ" href="about.aspx">About</a>
</li>
<li>
<a title="How to get in touch with us" href="contact.aspx">Contact</a>
</li>
</ul>

This will show the description text as a title when you mouseover each link. But we can take this a step further as the ASP.NET site map files support custom attributes. Lets add an access key so that users can navigate using their keyboard:

<sitemap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx"
        title="Home" description="Return to homepage" accesskey="H">
<sitemapnode url="about.aspx"
        title="About" description="About company XYZ" accesskey="A">
<sitemapnode url="contact.aspx" title="Contact"
        description="How to get in touch with us" accesskey="C">
</sitemapnode>
</sitemapnode>

Custom attributes are accessed via an indexer e.g.

// node is a SiteMapNode instance
string innerText = node.Title; // accesses the Title property
string shortcut = node["accessKey"]; // accesses the accessKey custom attribute

We can add this to our markup like this:

<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
      ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
      DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>" title="<%# Eval("description") %>"
                accesskey="<%# Eval("[accessKey]") %>">
<%# Eval("title") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>

This will render the following HTML:

<ul>
<li>
<a accesskey="A" title="About company XYZ" href="about.aspx">About</a>
</li>
<li>
<a accesskey="C" title="How to get in touch with us" href="contact.aspx">Contact</a>
</li>
</ul>

If you fancy playing around with the above sample code please feel free to download it.

Hope you find this useful.


UPDATE: I have moved my blog to my own domain and this post now be found here.

This is as much a note for me as anything as every time I need to data-bind a control to a Hashtable or a Dictionary<TKey, TValue> I always find myself reaching for Google.

When data-binding to a key-value based collection you output data as follows:

  • <%# Eval("Key") %> or
  • <%# Eval("Value") %>

Simple really.


UPDATE: I have moved my blog to my own domain and this post now be found here.

If you want to have a go with IronPython on Silverlight 2.0, you could do worse than to follow the advice in this article. Although the example is rather simplistic it should get you up and running in no time.

As the Chiron tool is in the Microsoft SDK’s folder, I am presuming that you will need to install the Silverlight 2.0 SDK first (as well as the Silverlight 2.0 runtime of course). These can be downloaded from here:

Have fun!


UPDATE: I have moved my blog to my own domain and this post now be found here.

If, like me, you fancied playing around with IronPython and Silverlight on Apache, here’s how I got mine set-up:

  1. Download and unpack DLRConsole.zip from http://silverlight.net/Samples/1.1/DLR-Console/DLRConsole.zip
  2. Added the following to my Apache httpd.conf file:
     <VirtualHost 127.0.0.1>
             ServerName DLRConsole
             DocumentRoot "C:/dev/DLRConsole"
             AddType text/python .py
             AddType text/jscript .jsx        
    
             DirectoryIndex index.htm index.html
    </VirtualHost>
    <Directory "C:/dev/DLRConsole">
             Options Indexes MultiViews
             AllowOverride None
             Order allow,deny
             Allow from all 
    
             DirectoryIndex index.htm index.html
    </Directory>
    
  3. I also set up the following in my hosts file (typically located on Windows at C:WINDOWSSYSTEM32DRIVERSETCHOSTS):
    127.0.0.1 dlrconsole
  4. Re-start Apache and point your browser to http://dlrconsole

You may need to comment out any other Python handlers in you httpd.conf file and add them on a per <Directory> basis, but obviously if you’ve got loads of Python-based sites on your Apache installation then your mileage may vary. Fortunately this wasn’t an issue for me so the following was sufficient:

AddHandler cgi-script .pl
#AddHandler cgi-script .py

For more on the DLRConsole see this article from MSDN Magazine.