<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>William Duffy</title>
	
	<link>http://www.wduffy.co.uk/blog</link>
	<description>Glasgow Based C# ASP.NET Web Developer</description>
	<lastBuildDate>Sun, 09 Dec 2012 20:54:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/wduffy" /><feedburner:info uri="wduffy" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Running SMTP services automatically on startup</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/KQ15uT3uXgE/</link>
		<comments>http://www.wduffy.co.uk/blog/running-smtp-services-automatically-on-startup/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 09:51:25 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[Windows Server]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[Service]]></category>
		<category><![CDATA[SMTP]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=805</guid>
		<description><![CDATA[Recently I setup a new server for a client that required smtp services running. Annoyingly though, whenever the server was rebooted the smtp service had to be manually started. Server restarts aren&#8217;t common to be fair, but when they do occur the configuration requirements should be zero. I needed the SMTP service to automatically start [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">R</span>ecently I setup a new server for a client that required smtp services running. Annoyingly though, whenever the server was rebooted the smtp service had to be manually started. Server restarts aren&#8217;t common to be fair, but when they do occur the configuration requirements should be zero.</p>
<p>I needed the SMTP service to automatically start running on reboot, and the solution was simple.</p>
<p>The SMTP service&#8217;s startup type is, by default, set to Manual. To alter this navigate to<strong> Start</strong> -&gt; <strong>Administrative Tools</strong> -&gt; <strong>Services</strong> and find the <strong>Simple Mail Transfer Protocol (SMTP)</strong> service.</p>
<p>Right click on the service and choose properties. A modal window should appear where you can change the startup type from <strong>Manual</strong> to <strong>Automatic</strong>.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/KQ15uT3uXgE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/running-smtp-services-automatically-on-startup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/running-smtp-services-automatically-on-startup/</feedburner:origLink></item>
		<item>
		<title>The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine.</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/plCftWjJPzE/</link>
		<comments>http://www.wduffy.co.uk/blog/the-microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 14:13:01 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[Windows Server]]></category>
		<category><![CDATA[Application Pool]]></category>
		<category><![CDATA[Jet]]></category>
		<category><![CDATA[x64]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=795</guid>
		<description><![CDATA[Recently I had to upgrade the server running one of my largest web applications and ran into an error that threw me a little. The web application allowed clients to upload excel spreadsheets that are read by an ADO DataAdapter and written to Sql Server. The old server was running Windows Server 2003, the new [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">R</span>ecently I had to upgrade the server running one of my largest web applications and ran into an error that threw me a little.</p>
<p>The web application allowed clients to upload excel spreadsheets that  are read by an ADO DataAdapter and written to Sql Server. The old server was running Windows Server 2003, the new server was  running Windows Server 2008. Everything worked fine on the old 2003  server, however the new 2008 server just wouldn&#8217;t play nice. On uploading an excel spreadsheet the following exception was being thrown</p>
<h2>The &#8216;Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine.</h2>
<p>Thankfully the solution to the problem was a simple one. IIS on Windows Server 2008 was running its application pool in 64 bit mode. There are no 64 bit Jet drivers. Simply changing the application pool to run in 32 bit mode enables support for the 32 bit Jet drivers.</p>
<p>To do this right click on the target <strong>application pool</strong> in IIS, select <strong>Advanced Settings</strong> and change <strong>Enable 32-Bit Applications</strong> to <strong>True.</strong></p>
<p>If you want to check that you have the latest version of the Jet drivers goto c:\Windows\SysWOW64, right click on the Msjet40.dll file, select the details tab and view the version number. At the time of writing the latest version was <strong>4.0.9704.0</strong>. You can view more details on this process at <a title="How to obtain the latest service pack for the Microsoft Jet 4.0 Database Engine" href="http://support.microsoft.com/kb/239114" target="_blank">http://support.microsoft.com/kb/239114</a></p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/plCftWjJPzE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/the-microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/the-microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine/</feedburner:origLink></item>
		<item>
		<title>Using Application_Error in ASP.NET MVC’s global.asax to Handle Errors</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/gIkLMaPcc_s/</link>
		<comments>http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 19:14:06 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Application_Error]]></category>
		<category><![CDATA[Error Handling]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=769</guid>
		<description><![CDATA[ASP.NET MVC&#8217;s controllers have great error handling capabilities and can be easily extended to cater for application specific requirements. However, many developer don&#8217;t want to manage errors at a controller level and would rather manage 99 percent of errors from a single location. This allows for a single point of error logging, notification and handling. [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">A</span>SP.NET MVC&#8217;s controllers have great error handling capabilities and can be easily extended to cater for application specific requirements. However, many developer don&#8217;t want to manage errors at a controller level and would rather manage 99 percent of errors from a single location. This allows for a single point of error logging, notification and handling.</p>
<p>The Application_Error event within the global.asax is the perfect place for this, but there are a few issues to consider when used in an MVC project.</p>
<ol>
<li>Server.Transfer is not available to serve an error message as it requires a physical file to serve. Your MVC project more than likely doesn&#8217;t have one of theses as it uses routes, controllers and views. We need to find a workaround for this in order to return suitable response headers.</li>
<li>Reponse.Redirect is not suitable as &#8217;500: Internal Server Error&#8217; and &#8217;404: Not Found&#8217; pages should serve a suitable response header, not a 301 redirect.</li>
<li>Response.Clear() should be called to ensure that any content written to the response stream before the error occurred is removed.</li>
<li>Server.ClearError() must be called to stop ASP.NET from serving the yellow screen of death.</li>
</ol>
<p>With these points in mind the following steps can be coded into the Application_Error event for error handling and logging.</p>
<ol>
<li>Get the last error raised.</li>
<li>Get the error code to respond with.</li>
<li>Log the error (I&#8217;m ignoring 404&#8242;s).</li>
<li>Clear the response stream.</li>
<li>Clear the server error.</li>
<li>Render the error handling controller without a redirect.</li>
</ol>
<p>Here is the Application_Error code&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">void</span> Application_Error<span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">object</span> sender, EventArgs e<span style="color: #f7f4ef;">&#41;</span>
<span style="color: #f7f4ef;">&#123;</span>
        <span style="color: #D6618C;">var</span> error <span style="color: #f7f4ef;">=</span> Server.<span style="color: #87bcd2;">GetLastError</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #D6618C;">var</span> code <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">&#40;</span>error <span style="color: #f7f4ef;">is</span> HttpException<span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">?</span> <span style="color: #f7f4ef;">&#40;</span>error <span style="color: #87BCD2;">as</span> HttpException<span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">GetHttpCode</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">:</span> <span style="color: #D6618C;">500</span><span style="color: #f7f4ef;">;</span>
&nbsp;
        <span style="color: #87BCD2;">if</span> <span style="color: #f7f4ef;">&#40;</span>code <span style="color: #f7f4ef;">!=</span> <span style="color: #D6618C;">404</span><span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Generate email with error details and send to administrator</span>
                <span style="color: #008080; font-style: italic;">// I'm using RazorMail which can be downloaded from the Nuget Gallery</span>
                <span style="color: #008080; font-style: italic;">// I also have an extension method on type Exception that creates a string representation</span>
                <span style="color: #D6618C;">var</span> email <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> RazorMailMessage<span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;Website Error&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                email.<span style="color: #87bcd2;">To</span>.<span style="color: #87bcd2;">Add</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;errors@wduffy.co.uk&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                email.<span style="color: #87bcd2;">Templates</span>.<span style="color: #87bcd2;">Add</span><span style="color: #f7f4ef;">&#40;</span>error.<span style="color: #87bcd2;">GetAsHtml</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">new</span> HttpRequestWrapper<span style="color: #f7f4ef;">&#40;</span>Request<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                Kernel.<span style="color: #87bcd2;">Get</span><span style="color: #f7f4ef;">&lt;</span>IRazorMailSender<span style="color: #f7f4ef;">&gt;</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">Send</span><span style="color: #f7f4ef;">&#40;</span>email<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
        Response.<span style="color: #87bcd2;">Clear</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        Server.<span style="color: #87bcd2;">ClearError</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
        <span style="color: #D6618C;">string</span> path <span style="color: #f7f4ef;">=</span> Request.<span style="color: #87bcd2;">Path</span><span style="color: #f7f4ef;">;</span>
        Context.<span style="color: #87bcd2;">RewritePath</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">string</span>.<span style="color: #87bcd2;">Format</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;~/Errors/Http{0}&quot;</span>, code<span style="color: #f7f4ef;">&#41;</span>, <span style="color: #87BCD2;">false</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        IHttpHandler httpHandler <span style="color: #f7f4ef;">=</span> <span style="color: #f7f4ef;">new</span> MvcHttpHandler<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        httpHandler.<span style="color: #87bcd2;">ProcessRequest</span><span style="color: #f7f4ef;">&#40;</span>Context<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        Context.<span style="color: #87bcd2;">RewritePath</span><span style="color: #f7f4ef;">&#40;</span>path, <span style="color: #87BCD2;">false</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<p>And here is the ErrorsController code. Notice each action sets the response status error code before rendering the view. <em>Be careful with this controller as any errors will result in an infinite loop between itself and the Application_Error event!</em></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> ErrorsController <span style="color: #f7f4ef;">:</span> Controller
<span style="color: #f7f4ef;">&#123;</span>
&nbsp;
        <span style="color: #f7f4ef;">&#91;</span>HttpGet<span style="color: #f7f4ef;">&#93;</span>
        <span style="color: #87BCD2;">public</span> ActionResult Http404<span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">string</span> source<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
                Response.<span style="color: #87bcd2;">StatusCode</span> <span style="color: #f7f4ef;">=</span> <span style="color: #D6618C;">404</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #87BCD2;">return</span> View<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
        <span style="color: #f7f4ef;">&#91;</span>HttpGet<span style="color: #f7f4ef;">&#93;</span>
        <span style="color: #87BCD2;">public</span> ActionResult Http500<span style="color: #f7f4ef;">&#40;</span><span style="color: #D6618C;">string</span> source<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
                Response.<span style="color: #87bcd2;">StatusCode</span> <span style="color: #f7f4ef;">=</span> <span style="color: #D6618C;">500</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #87BCD2;">return</span> View<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/gIkLMaPcc_s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/</feedburner:origLink></item>
		<item>
		<title>Resetting the WordPress Admin Password</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/p66VI7npft4/</link>
		<comments>http://www.wduffy.co.uk/blog/resetting-wordpress-admin-password/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 21:48:06 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MD5]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=736</guid>
		<description><![CDATA[I support a lot of WordPress systems and find a common occurence to be adminstrators forgetting their password. In order to reset the admin password I run the following simple SQL query against the WordPress database using MySql query browser. Replace your_database and your_password as required. UPDATE your_database.wp_users SET user_pass = MD5&#40;'your_password'&#41; WHERE user_login = [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span> support a lot of WordPress systems and find a common occurence to be adminstrators forgetting their password.</p>
<p>In order to reset the admin password I run the following simple SQL query against the WordPress database using MySql query browser.</p>
<p>Replace <em>your_database</em> and <em>your_password</em> as required.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">UPDATE
	your_database.<span style="color: #87bcd2;">wp_users</span>
SET
	user_pass <span style="color: #f7f4ef;">=</span> MD5<span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">'your_password'</span><span style="color: #f7f4ef;">&#41;</span>
WHERE
	user_login <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">'admin'</span><span style="color: #f7f4ef;">;</span></pre></div></div>

<p>Note: You can of course use the snippet above for any user, but if it&#8217;s not the admin password requiring a reset perhaps using the login area will be easier.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/p66VI7npft4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/resetting-wordpress-admin-password/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/resetting-wordpress-admin-password/</feedburner:origLink></item>
		<item>
		<title>Clearing ASP.NET Cache</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/hpuAmcnwZhQ/</link>
		<comments>http://www.wduffy.co.uk/blog/clearing-asp-net-cache/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 21:09:39 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Caching]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=721</guid>
		<description><![CDATA[ASP.NET&#8217;s caching system is invaluable for speeding up your website and reducing your server/database load. However, sometimes you will find a need to manually clear the cache and allow it to rebuild with fresh data. As simple as this seems it can prove trickier than you would expect. The following code snippet allows you to [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">A</span>SP.NET&#8217;s caching system is invaluable for speeding up your website and reducing your server/database load. However, sometimes you will find a need to manually clear the cache and allow it to rebuild with fresh data. As simple as this seems it can prove trickier than you would expect.</p>
<p>The following code snippet allows you to do just this. Simply create a generic handler somewhere accessible on your website to call when you need to clear the cache. For example www.yoursite.com/clearcache.ashx.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">using</span> <span style="color: #008080;">System</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #f7f4ef;">;</span>
<span style="color: #87BCD2;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #f7f4ef;">;</span>
&nbsp;
<span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">class</span> ClearCache <span style="color: #f7f4ef;">:</span> IHttpHandler
<span style="color: #f7f4ef;">&#123;</span>
    <span style="color: #87BCD2;">public</span> <span style="color: #D6618C;">bool</span> IsReusable
    <span style="color: #f7f4ef;">&#123;</span>
        get <span style="color: #f7f4ef;">&#123;</span> <span style="color: #87BCD2;">return</span> true<span style="color: #f7f4ef;">;</span> <span style="color: #f7f4ef;">&#125;</span>
    <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
    <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">void</span> ProcessRequest<span style="color: #f7f4ef;">&#40;</span>HttpContext context<span style="color: #f7f4ef;">&#41;</span>
    <span style="color: #f7f4ef;">&#123;</span>
        <span style="color: #87BCD2;">foreach</span> <span style="color: #f7f4ef;">&#40;</span>DictionaryEntry item <span style="color: #87BCD2;">in</span> context.<span style="color: #87bcd2;">Cache</span><span style="color: #f7f4ef;">&#41;</span>
            context.<span style="color: #87bcd2;">Cache</span>.<span style="color: #87bcd2;">Remove</span><span style="color: #f7f4ef;">&#40;</span>item.<span style="color: #87bcd2;">Key</span> <span style="color: #87BCD2;">as</span> <span style="color: #D6618C;">string</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
        context.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">ContentType</span> <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;text/html&quot;</span><span style="color: #f7f4ef;">;</span>
        context.<span style="color: #87bcd2;">Response</span>.<span style="color: #87bcd2;">Write</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;Cache cleared.&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
    <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/hpuAmcnwZhQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/clearing-asp-net-cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/clearing-asp-net-cache/</feedburner:origLink></item>
		<item>
		<title>Alternating Row Colours in MVC Views</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/fGcxJZEkBck/</link>
		<comments>http://www.wduffy.co.uk/blog/alternating-row-colours-in-mvc-views/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 12:02:34 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[IList]]></category>
		<category><![CDATA[ViewModel]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=707</guid>
		<description><![CDATA[Alternating row colours on tabular data is one of the most common data visualisation styles a web developer has to implement. Almost every project I have built required tabular data, and alternating row colours were key to the clean, easily readable presentation of that data. However, it quickly becomes tiresome writing the logic to work [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">A</span>lternating row colours on tabular data is one of the most common data visualisation styles a web developer has to implement. Almost every project I have built required tabular data, and alternating row colours were key to the clean, easily readable presentation of that data.</p>
<p>However, it quickly becomes tiresome writing the logic to work out which rows are odd/even and change css classes on every table/project, not to mention it breaks the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> principle. A further issue arises from the MVC pattern&#8217;s recommendation that views should be dumb and have low <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity" target="_blank">cyclomatic complexity</a>.</p>
<p>To get around both these issues, and in the process gain an abstracted, reusable method of alternating the row colours we can create an extension method. This extension method, when implemented against the IList interface, allows for its use on any concrete collection which implements that interface.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System.<span style="color: #87bcd2;">Collections</span></span>
<span style="color: #f7f4ef;">&#123;</span>
  <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">class</span> IListExtensions
   <span style="color: #f7f4ef;">&#123;</span>
&nbsp;
    <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">string</span> GetRowClass<span style="color: #f7f4ef;">&#40;</span><span style="color: #87BCD2;">this</span> IList list, <span style="color: #D6618C;">object</span> item, <span style="color: #D6618C;">string</span> evenClass, <span style="color: #D6618C;">string</span> oddClass <span style="color: #f7f4ef;">=</span> <span style="color: #acacac;">&quot;&quot;</span><span style="color: #f7f4ef;">&#41;</span>
     <span style="color: #f7f4ef;">&#123;</span>
      <span style="color: #D6618C;">int</span> index <span style="color: #f7f4ef;">=</span> list.<span style="color: #87bcd2;">IndexOf</span><span style="color: #f7f4ef;">&#40;</span>item<span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
      <span style="color: #87BCD2;">return</span> index <span style="color: #f7f4ef;">%</span> <span style="color: #D6618C;">2</span> <span style="color: #f7f4ef;">==</span> <span style="color: #D6618C;">0</span> <span style="color: #f7f4ef;">?</span> evenClass <span style="color: #f7f4ef;">:</span> oddClass<span style="color: #f7f4ef;">;</span>
     <span style="color: #f7f4ef;">&#125;</span>
&nbsp;
   <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<p>To alternate row colours in your view it&#8217;s simply a case of calling the extension method and passing in the relevant classes for odd/even rows.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;table</span> <span style="color: #87BCD2;">class</span>=<span style="color: #ACACAC;">&quot;formattedTable&quot;</span> <span style="color: #87BCD2;">cellspacing</span>=<span style="color: #ACACAC;">&quot;0&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;tr<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>Name<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>Business<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>Email<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/th<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/tr<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #FFFFFF;">&lt;% foreach <span style="color: #66cc66;">&#40;</span>var c in Model.Contacts<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> %<span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;tr</span> <span style="color: #87BCD2;">class</span>=<span style="color: #ACACAC;">&quot;&lt;%: Model.Contacts.GetRowClass(c, &quot;</span>evenRow<span style="color: #ACACAC;">&quot;, &quot;</span>oddRow<span style="color: #ACACAC;">&quot;) %&gt;</span></span>&quot;&gt;
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span>&lt;%: c.Fullname %<span style="color: #D6618C; font-weight: bold;">&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;/td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span>&lt;%: c.Business %<span style="color: #D6618C; font-weight: bold;">&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;/td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span>&lt;%: c.Email<span style="color: #66cc66;">&#41;</span>%<span style="color: #D6618C; font-weight: bold;">&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;/td<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/tr<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #FFFFFF;">&lt;% <span style="color: #66cc66;">&#125;</span> %<span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/table<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I have put the extension method into the System.Collection namespace so that I only have to reference my extension method library in the project to access it, however if you are uncomfortable with this you can easily put it into your own namespace and import it manually.</p>
<p>Also, notice that I am using C# 4&#8242;s optional parameters on the oddClass parameter. A lot of times I only style one row and leave the other transparent so this allows for slightly neater view markup. If you aren&#8217;t using C# 4 you can simply overload the extension method.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/fGcxJZEkBck" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/alternating-row-colours-in-mvc-views/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/alternating-row-colours-in-mvc-views/</feedburner:origLink></item>
		<item>
		<title>Modifing XML values in XSLT documents</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/WkFIKaXB1lI/</link>
		<comments>http://www.wduffy.co.uk/blog/modifing-xml-values-in-xslt-documents/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 22:11:58 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[XML]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Strings]]></category>
		<category><![CDATA[XPath]]></category>
		<category><![CDATA[XQuery]]></category>
		<category><![CDATA[XSL]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=361</guid>
		<description><![CDATA[Ever wondered how to perform manipulations of XML values in an XSLT document? For example, if you were consuming an RSS feed which had description values greater than 500 characters in length, but you only wanted to display the first 50 characters before leading the user through to the full post. The solution is XPath [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">E</span>ver wondered how to perform manipulations of XML values in an XSLT document?</p>
<p>For example, if you were consuming an RSS feed which had description values greater than 500 characters in length, but you only wanted to display the first 50 characters before leading the user through to the full post.</p>
<p>The solution is XPath functions. Much like any general level scripting language such as VBScript or PHP, XPath makes available a range of functions to help you achieve basic type manipulation and querying.</p>
<p>Lets elaborate on the RSS example. Imagine you were consuming a feed which looked similar to the following <em>(trimmed for simplication)</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;?xml</span> <span style="color: #87BCD2;">version</span>=<span style="color: #ACACAC;">&quot;1.0&quot;</span> <span style="color: #87BCD2;">encoding</span>=<span style="color: #ACACAC;">&quot;UTF-8&quot;</span><span style="color: #D6618C; font-weight: bold;">?&gt;</span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;rss<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;channel<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;item<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;title<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>Modifing XML values in XSLT documents<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/title<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;link<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>http://feedproxy.google.com/~r/wduffy/~3/WkFIKaXB1lI/<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/link<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;description<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span><span style="color: #FFFFFF;">&lt;![CDATA[Ever wondered how to perform manipulations of XML values in an XSLT document?</span>
<span style="color: #FFFFFF;">For example, if you were consuming an RSS feed which had description values greater than 500 characters in length, but you only wanted to display the first 50 characters before leading the user through to the full post.</span>
<span style="color: #FFFFFF;">The solution is XPath functions. Much [...]]]&gt;</span><span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/description<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/item<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/channel<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/rss<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You wish to render a short preview of the latest blog post in a format similar to the following example, perhaps on a website&#8217;s &#8220;latest blog posts&#8221; widget.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;strong<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>Modifing XML values in XSLT documents<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/strong<span style="color: #D6618C; font-weight: bold;">&gt;</span></span><span style="color: #D6618C; font-weight: bold;">&lt;br</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span></span>
Ever wondered how to perform manipulations of XML...<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;br</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;a</span> <span style="color: #87BCD2;">href</span>=<span style="color: #ACACAC;">&quot;http://feedproxy.google.com/~r/wduffy/~3/WkFIKaXB1lI/&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>Read the full post!<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/a<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The XSLT document would be formatted as below. Notice the call to substring() when requesting the value of the xml document&#8217;s description field. This function extracts a new value from the XML documents description value starting at position 0, and finishing at position 50.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;?xml</span> <span style="color: #87BCD2;">version</span>=<span style="color: #ACACAC;">&quot;1.0&quot;</span> <span style="color: #87BCD2;">encoding</span>=<span style="color: #ACACAC;">&quot;ISO-8859-1&quot;</span><span style="color: #D6618C; font-weight: bold;">?&gt;</span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #87BCD2;">version</span>=<span style="color: #ACACAC;">&quot;1.0&quot;</span> <span style="color: #87BCD2;">xmlns:xsl</span>=<span style="color: #ACACAC;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:template</span> <span style="color: #87BCD2;">match</span>=<span style="color: #ACACAC;">&quot;/&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #87BCD2;">select</span>=<span style="color: #ACACAC;">&quot;/rss/channel/item&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
   <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:if</span> <span style="color: #87BCD2;">test</span>=<span style="color: #ACACAC;">&quot;position() &amp;lt;= 1&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span></span>
&nbsp;
       <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;strong<span style="color: #D6618C; font-weight: bold;">&gt;</span></span><span style="color: #D6618C; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #87BCD2;">select</span>=<span style="color: #ACACAC;">&quot;title&quot;</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;/strong<span style="color: #D6618C; font-weight: bold;">&gt;</span></span><span style="color: #D6618C; font-weight: bold;">&lt;br</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span></span>
       <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #87BCD2;">select</span>=<span style="color: #ACACAC;">&quot;substring(description, 0, 50)&quot;</span> <span style="color: #87BCD2;">disable-output-escaping</span>=<span style="color: #ACACAC;">&quot;yes&quot;</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span></span>...<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;br</span> <span style="color: #D6618C; font-weight: bold;">/&gt;</span></span>
&nbsp;
       <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;a<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #87BCD2;">name</span>=<span style="color: #ACACAC;">&quot;href&quot;</span><span style="color: #D6618C; font-weight: bold;">&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #87BCD2;">select</span>=<span style="color: #ACACAC;">&quot;link&quot;</span><span style="color: #D6618C; font-weight: bold;">/&gt;</span><span style="color: #D6618C; font-weight: bold;">&lt;/xsl:attribute<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
        Read the full story
       <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/a<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/xsl:if<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/xsl:for-each<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/xsl:template<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span>
<span style="color: #FFFFFF;"><span style="color: #D6618C; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #D6618C; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Having this level of control over the transformation process allows for a huge range of possibilities and doesn&#8217;t only apply to basic string manipulation. Functions exist for date types, numeric types, boolean types and nodes. For more details on exactly what is available check out this excellent overview of <a href="http://www.w3schools.com/xpath/xpath_functions.asp" target="_blank">XPath, XQuery, and XSLT Functions</a> on W3 Schools.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/WkFIKaXB1lI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/modifing-xml-values-in-xslt-documents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/modifing-xml-values-in-xslt-documents/</feedburner:origLink></item>
		<item>
		<title>Tracking Google Goals With No URL Using jQuery</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/FXFGm71otKE/</link>
		<comments>http://www.wduffy.co.uk/blog/tracking-google-goals-with-no-url-using-jquery/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 13:42:27 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Google Goals]]></category>
		<category><![CDATA[Internet Marketing]]></category>
		<category><![CDATA[Search Engine Optimisation]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=613</guid>
		<description><![CDATA[Google Goals make it extremely easy to track successful interaction with your website by setting a goal based on the serving of a URL to the client. However, what if a goal is a user clicking on a mailto email link, or clicking download to receive a pdf of your brochure? Neither of these resources [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">G</span>oogle Goals make it extremely easy to track successful interaction with your website by setting a goal based on the serving of a URL to the client. However, what if a goal is a user clicking on a mailto email link, or clicking download to receive a pdf of your brochure? Neither of these resources can run JavaScript to call Google Analytics and track the goal.</p>
<p>The solutions is to create a Google Goal with a fake url and manually call this on the user&#8217;s interaction with the element leading them to the resource. For example, if I wanted to track a user clicking the link &lt;a href=&#8221;mailto:your@email.com&#8221;&gt;Contact Me&lt;/a&gt; to engage in dialogue, I would create a goal whose url is <strong>/G1/Email-Enquiry</strong>. If I also wanted to track a user clicking &lt;a href=&#8221;/downloads/my-brochure.pdf&#8221;&gt;Download My Brochure&lt;/a&gt; to get more details, I would create a goal whose url is <strong>/G2/Brochure-Download</strong>. Neither of these urls exist.</p>
<p><em>To set up a goal go to your Google Analytics account > click Edit next to the profile you wish to add the Google Goal to > click Add Goal in the goals area.</em></p>
<p><img src="http://www.wduffy.co.uk/blog/wp-content/uploads/2010/08/google-goal.gif" alt="Adding a Google Goal" title="Adding a Google Goal" width="600" height="816" class="alignnone size-full wp-image-616" /></p>
<h3>IMPORTANT: Remember to include your Google Analytics tracking code on your page before continuing if you have not already done so! It must be placed at the top of your page above all manual calls to the Google API for everything to work.</h3>
<p>Once your goals are setup you simply need to request your fake url when the user interacts with your goal. Fortunately _gaq.push() is already setup to easily handle this. <em>_gaq.push() is the Google API method used to send tracking information to Google Analytics, it will normally look like the following.</em></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #D6618C;">var</span> _gaq <span style="color: #f7f4ef;">=</span> _gaq <span style="color: #f7f4ef;">||</span> <span style="color: #f7f4ef;">&#91;</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">;</span>
_gaq.<span style="color: #87bcd2;">push</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">'_setAccount'</span>, <span style="color: #acacac;">'UA-2305701-34'</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
_gaq.<span style="color: #87bcd2;">push</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">'_trackPageview'</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span></pre></div></div>

<p>With this method available we can easily track user interaction on an element by passing our custom goal urls in an onclick event handler. See the following demonstration as an example.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
   <span style="color: #f7f4ef;">&lt;</span>a href<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;mailto:your@email.com&quot;</span> onclick<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;_gaq.push(['_trackPageview', '/G1/Email-Enquiry']);&quot;</span><span style="color: #f7f4ef;">&gt;</span>Contact Me<span style="color: #f7f4ef;">&lt;/</span>a<span style="color: #f7f4ef;">&gt;</span>
&nbsp;
   <span style="color: #f7f4ef;">&lt;</span>a href<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;/downloads/my-brochure.pdf&quot;</span> onclick<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;_gaq.push(['_trackPageview', '/G2/Brochure-Download']);&quot;</span><span style="color: #f7f4ef;">&gt;</span>Download My Brochure<span style="color: #f7f4ef;">&lt;/</span>a<span style="color: #f7f4ef;">&gt;</span></pre></div></div>

<p>Everything should be working great at this point. However, this implementation can become difficult to maintain/scale when multiple interactions are regarded as the same goal. For example, if you have tens, hundreds, even thousands of those links scattered throughout your site you will need to manually attach the Google Goal to every one of them. </p>
<p>To work around this manual process we can use jQuery selectors to assess the dom when the page is ready, attaching the call to Google Analytics on each element&#8217;s click event. The selectors below find all <strong>a</strong> elements whose <strong>href</strong> attribute <strong>contains</strong> the specified value.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #f7f4ef;">&lt;</span>script type<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;text/javascript&quot;</span> src<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;/js/jquery-1.4.1.min.js&quot;</span><span style="color: #f7f4ef;">&gt;&lt;/</span>script<span style="color: #f7f4ef;">&gt;</span> <span style="color: #008080; font-style: italic;">// Download your copy at jQuery.com</span>
<span style="color: #f7f4ef;">&lt;</span>script type<span style="color: #f7f4ef;">=</span><span style="color: #acacac;">&quot;text/javascript&quot;</span><span style="color: #f7f4ef;">&gt;</span>
    $<span style="color: #f7f4ef;">&#40;</span>function<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span> <span style="color: #f7f4ef;">&#123;</span> <span style="color: #008080; font-style: italic;">// Set Google Goals</span>
        $<span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;a[href*=your@email.com]&quot;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">click</span><span style="color: #f7f4ef;">&#40;</span>function<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#123;</span>_gaq.<span style="color: #87bcd2;">push</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">'_trackPageview'</span>, <span style="color: #acacac;">'/G1/Email-Enquiry'</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span><span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
        $<span style="color: #f7f4ef;">&#40;</span><span style="color: #acacac;">&quot;a[href*=my-brochure.pdf]&quot;</span><span style="color: #f7f4ef;">&#41;</span>.<span style="color: #87bcd2;">click</span><span style="color: #f7f4ef;">&#40;</span>function<span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">&#123;</span>_gaq.<span style="color: #87bcd2;">push</span><span style="color: #f7f4ef;">&#40;</span><span style="color: #f7f4ef;">&#91;</span><span style="color: #acacac;">'_trackPageview'</span>, <span style="color: #acacac;">'/G2/Brochure-Download'</span><span style="color: #f7f4ef;">&#93;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span><span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
    <span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
<span style="color: #f7f4ef;">&lt;/</span>script<span style="color: #f7f4ef;">&gt;</span></pre></div></div>

<p>If you have different requirements for your selectors or for more information on jQuery selectors check out the <a href="http://api.jquery.com/category/selectors/" target="_blank">jQuery documentation</a>.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/FXFGm71otKE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/tracking-google-goals-with-no-url-using-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/tracking-google-goals-with-no-url-using-jquery/</feedburner:origLink></item>
		<item>
		<title>Ordinal Suffix DateTime Extension Method</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/jX6GU9S1eOs/</link>
		<comments>http://www.wduffy.co.uk/blog/ordinal-suffix-datetime-extension-method/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 20:33:41 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[DateTime]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=594</guid>
		<description><![CDATA[In my opinion the most annoying missing piece of date functionality in the .NET framework is an ability to get a DateTime&#8217;s ordinal suffix. If you&#8217;re as disheartened as me with this then dismay no more. Below is my DateTime extension method that returns the ordinal suffix for the current day of the month. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span>n my opinion the most annoying missing piece of date functionality in the .NET framework is an ability to get a DateTime&#8217;s ordinal suffix. If you&#8217;re as disheartened as me with this then dismay no more. Below is my DateTime extension method that returns the ordinal suffix for the current day of the month.</p>
<p>I&#8217;ve put the extension directly into the System namespace, but you can put it somewhere else if you prefer. Now if only we could get this into the frameworks inbuilt date formatting strings, that would be cool!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #87BCD2;">namespace</span> <span style="color: #f7f4ef;">System</span>
<span style="color: #f7f4ef;">&#123;</span>
        <span style="color: #008080; font-style: italic;">///&lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">///Returns the ordinal suffix for the day of the month represented by this instance</span>
        <span style="color: #008080; font-style: italic;">///&lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">///&lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #87BCD2;">public</span> <span style="color: #87BCD2;">static</span> <span style="color: #D6618C;">string</span> OrdinalSuffix<span style="color: #f7f4ef;">&#40;</span><span style="color: #87BCD2;">this</span> DateTime datetime<span style="color: #f7f4ef;">&#41;</span>
        <span style="color: #f7f4ef;">&#123;</span>
            <span style="color: #D6618C;">int</span> day <span style="color: #f7f4ef;">=</span> datetime.<span style="color: #87bcd2;">Day</span><span style="color: #f7f4ef;">;</span>
&nbsp;
            <span style="color: #87BCD2;">if</span> <span style="color: #f7f4ef;">&#40;</span>day <span style="color: #f7f4ef;">%</span> <span style="color: #D6618C;">100</span> <span style="color: #f7f4ef;">&gt;=</span> <span style="color: #D6618C;">11</span> <span style="color: #f7f4ef;">&amp;&amp;</span> day <span style="color: #f7f4ef;">%</span> <span style="color: #D6618C;">100</span> <span style="color: #f7f4ef;">&lt;=</span> <span style="color: #D6618C;">13</span><span style="color: #f7f4ef;">&#41;</span>
                <span style="color: #87BCD2;">return</span> <span style="color: #D6618C;">String</span>.<span style="color: #87bcd2;">Concat</span><span style="color: #f7f4ef;">&#40;</span>day, <span style="color: #acacac;">&quot;th&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
&nbsp;
            <span style="color: #87BCD2;">switch</span> <span style="color: #f7f4ef;">&#40;</span>day <span style="color: #f7f4ef;">%</span> <span style="color: #D6618C;">10</span><span style="color: #f7f4ef;">&#41;</span>
            <span style="color: #f7f4ef;">&#123;</span>
                <span style="color: #87BCD2;">case</span> <span style="color: #D6618C;">1</span><span style="color: #f7f4ef;">:</span>
                    <span style="color: #87BCD2;">return</span> <span style="color: #D6618C;">String</span>.<span style="color: #87bcd2;">Concat</span><span style="color: #f7f4ef;">&#40;</span>day, <span style="color: #acacac;">&quot;st&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #87BCD2;">case</span> <span style="color: #D6618C;">2</span><span style="color: #f7f4ef;">:</span>
                    <span style="color: #87BCD2;">return</span> <span style="color: #D6618C;">String</span>.<span style="color: #87bcd2;">Concat</span><span style="color: #f7f4ef;">&#40;</span>day, <span style="color: #acacac;">&quot;nd&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #87BCD2;">case</span> <span style="color: #D6618C;">3</span><span style="color: #f7f4ef;">:</span>
                    <span style="color: #87BCD2;">return</span> <span style="color: #D6618C;">String</span>.<span style="color: #87bcd2;">Concat</span><span style="color: #f7f4ef;">&#40;</span>day, <span style="color: #acacac;">&quot;rd&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
                <span style="color: #87BCD2;">default</span><span style="color: #f7f4ef;">:</span>
                    <span style="color: #87BCD2;">return</span> <span style="color: #D6618C;">String</span>.<span style="color: #87bcd2;">Concat</span><span style="color: #f7f4ef;">&#40;</span>day, <span style="color: #acacac;">&quot;th&quot;</span><span style="color: #f7f4ef;">&#41;</span><span style="color: #f7f4ef;">;</span>
            <span style="color: #f7f4ef;">&#125;</span>
        <span style="color: #f7f4ef;">&#125;</span>
<span style="color: #f7f4ef;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/wduffy/~4/jX6GU9S1eOs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/ordinal-suffix-datetime-extension-method/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/ordinal-suffix-datetime-extension-method/</feedburner:origLink></item>
		<item>
		<title>Repopulate Html.PasswordFor on validation errors</title>
		<link>http://feedproxy.google.com/~r/wduffy/~3/ChG4Ca_XdGE/</link>
		<comments>http://www.wduffy.co.uk/blog/repopulate-html-passwordfor-on-validation-errors/#comments</comments>
		<pubDate>Mon, 31 May 2010 13:28:27 +0000</pubDate>
		<dc:creator>William</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HTML Helpers]]></category>
		<category><![CDATA[Html.PasswordFor]]></category>
		<category><![CDATA[postback]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.wduffy.co.uk/blog/?p=574</guid>
		<description><![CDATA[ASP.NET MVC&#8217;s HTML helpers are fast and efficient for rendering elements using your model. One of these is the HTML.PasswordFor&#60;&#62; method, which has a little gotcha that many people get caught out with. The HTML.PasswordFor&#60;&#62; method will render a password input to mask keystrokes on the browser but will not populate the value using the [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">A</span>SP.NET MVC&#8217;s HTML helpers are fast and efficient for rendering elements using your model. One of these is the HTML.PasswordFor&lt;&gt; method, which has a little gotcha that many people get caught out with.</p>
<p>The HTML.PasswordFor&lt;&gt; method will render a password input to mask keystrokes on the browser but will not populate the value using the model. For most scenarios this is fine however there are occasions this functionality can be counter productive.</p>
<p>For example, a logged in user may be given the opportunity to update their password. A suitable implementation of this would include three fields in the process.</p>
<ol>
<li>Current Password</li>
<li>New Password</li>
<li>Confirm New Password</li>
</ol>
<p>All these fields will be keystroke masked password fields. When the post to your action occurs the model will be validated to ensure the following restrictions are met</p>
<ol>
<li><em>Current Password </em>is supplied and is correct</li>
<li><em>New password </em>is supplied and is within acceptable criteria</li>
<li><em>Confirm New Password </em>matches <em>New Password </em>exactly</li>
</ol>
<p>Any of these criteria can fail, at which point the action will return its view with suitable validation errors. <strong>The problem exists at this point as the password field will not be repopulated with the last entered value, instead being reset to blank</strong>. Fortunately the solution, although not instantly obvious, is very easy. Simply populate the value manually using Html.PasswordFor&#8217;s htmlAttributes parameter and your model.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Html.<span style="color: #87bcd2;">PasswordFor</span><span style="color: #f7f4ef;">&#40;</span>x <span style="color: #f7f4ef;">=&gt;</span> x.<span style="color: #87bcd2;">CurrentPassword</span>, <span style="color: #f7f4ef;">new</span> <span style="color: #f7f4ef;">&#123;</span> value <span style="color: #f7f4ef;">=</span> Model.<span style="color: #87bcd2;">CurrentPassword</span> <span style="color: #f7f4ef;">&#125;</span><span style="color: #f7f4ef;">&#41;</span></pre></div></div>

<p>The user can now continue to enter their details from the point that validation failed.</p>
<p><strong>Important Note</strong>. Do not pass your original domain model to the view model if you are going to use this method. If you do, the password will always be pre-populated. The view model should have it&#8217;s own properties to handle the three password entries and be empty by default.</p>
<img src="http://feeds.feedburner.com/~r/wduffy/~4/ChG4Ca_XdGE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.wduffy.co.uk/blog/repopulate-html-passwordfor-on-validation-errors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.wduffy.co.uk/blog/repopulate-html-passwordfor-on-validation-errors/</feedburner:origLink></item>
	</channel>
</rss>
