<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>techencoder &#187; .Net</title>
	<atom:link href="http://techencoder.com/index.php/category/programming/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://techencoder.com</link>
	<description>Technical ideas in a human readable format</description>
	<lastBuildDate>Tue, 31 Aug 2010 02:15:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>		<item>
		<title>Intellisense for NAnt Build Files (VS 2005 and 2008)</title>
		<link>http://techencoder.com/index.php/2010/01/vs-intellisense-for-nant/</link>
		<comments>http://techencoder.com/index.php/2010/01/vs-intellisense-for-nant/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 05:05:06 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[NAnt]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=1050</guid>
		<description><![CDATA[Step 1. Make VS recognize a .build file as an XML file. Right-click a .build file in the &#8220;Solution Explorer&#8221; and select &#8220;Open With &#8230;&#8221; In the &#8220;Open With&#8221; dialog, select &#8220;XML Editor&#8221; and &#8220;Set as Default&#8221; Step 2: Enable intellisense for .build files. Get the schema file for NAnt: Version 0.86 Beta 1 is at [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://techencoder.com/wp-content/uploads/2010/01/nant.jpg" alt="nant" title="nant" width="600" height="106" class="aligncenter size-full wp-image-1100" /></p>
<h4>Step 1. Make VS recognize a .build file as an XML file.</h4>
<div>
<ol>
<li>Right-click a .build file in the &#8220;Solution Explorer&#8221; and select &#8220;Open With &#8230;&#8221;</li>
<li>In the &#8220;Open With&#8221; dialog, select &#8220;XML Editor&#8221; and &#8220;Set as Default&#8221;</li>
</ol>
<p><img class="aligncenter size-full wp-image-1053" title="vs-2005-2008-open-with-dialog" src="http://techencoder.com/wp-content/uploads/2010/01/vs-2005-2008-open-with-dialog.png" alt="vs-2005-2008-open-with-dialog" width="600" height="244" /></div>
<h4>Step 2: Enable intellisense for .build files.</h4>
<p>Get the schema file for NAnt:</p>
<ul>
<li>Version 0.86 Beta 1 is at <a title="XSD Schema for NAnt" href="http://nant.sf.net/release/0.86-beta1/nant.xsd" target="_blank">http://nant.sf.net/release/0.86-beta1/nant.xsd</a></li>
<li>Version 0.85 is at <a title="XSD Schema for NAnt" href="http://nant.sf.net/release/0.86-beta1/nant.xsd" target="_blank">http://nant.sf.net/release/0.85/nant.xsd</a></li>
</ul>
<p>Copy it to the &#8220;<em>xml\Schemas</em>&#8221; subdirectory of your VS installation:</p>
<div>
<ul>
<li>Note that you can check this path via <em>Tools &gt; Options &gt; Text Editor &gt; XML &gt; Miscellaneous</em></li>
</ul>
<p><img class="aligncenter size-full wp-image-1066" title="schema-location-option" src="http://techencoder.com/wp-content/uploads/2010/01/schema-location-option.png" alt="schema-location-option" width="600" height="347" /></div>
<div>Specify the schema to use within Visual Studio:</div>
<div>
<ul>
<li>Open the file so that it appears in a Visual Studio editor window.</li>
<li>Click the file&#8217;s editor window (to activate it) and specify the schema to use (within the &#8220;Properties&#8221; window).<br />
<img class="aligncenter size-full wp-image-1057" title="change-schema-properties" src="http://techencoder.com/wp-content/uploads/2010/01/change-schema-properties.png" alt="change-schema-properties" width="600" height="148" /></li>
</ul>
<ul>
<li>Note that this dialog has changed from VS 2005 to 2008:</li>
</ul>
<p style="text-align: center;"><img class="size-full wp-image-1058" title="xsd-schemas-vs-2005" src="http://techencoder.com/wp-content/uploads/2010/01/xsd-schemas-vs-2005.png" alt="Visual Studio 2005" width="600" height="231" /><br />
(VS 2005)</p>
<p><img class="size-full wp-image-1059" title="xsd-schemas-vs-2008" src="http://techencoder.com/wp-content/uploads/2010/01/xsd-schemas-vs-2008.png" alt="Visual Studio 2008" width="600" height="235" /><br />
(VS 2008)</p>
<p>If all of that is not enough,  make sure to <strong>enter an xmlns and make sure it is equal to the xmlns specified in the XSD schema</strong>. </p>
<p>
!!!!! Even though the schema is available at <a title="NANT XML Schema" href="http://nant.sourceforge.net/release/0.86-beta1/nant.xsd" target="_blank">http://nant.sf.net/release/0.86-beta1/nant.xsd</a> and <a title="NANT XML Schema" href="http://nant.sourceforge.net/release/0.86-beta1/nant.xsd" target="_blank">http://nant.sourceforge.net/release/0.86-beta1/nant.xsd</a>, you must reference the former url in your build file because it is the one referenced in nant.xsd. !!!!!</p>
<p>Here is an example of what I mean:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://nant.sf.net/release/0.86-beta1/nant.xsd&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyProject&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- stuff here --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Hope that helps someone.  Happy Programming!</p></div>
<hr/>
<div xmlns:cc="http://creativecommons.org/ns#" about="http://www.flickr.com/photos/kubina/521327318/">
Attribution: The <a href='http://www.flickr.com/photos/kubina/521327318/'>ant photo</a> in this post is used with permission (<a rel="license" href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA 2.0</a>) by <a rel="cc:attributionURL" href="http://www.flickr.com/photos/kubina/">Jeff Kubina</a></div>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2010/01/vs-intellisense-for-nant/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting DataTables to work with ASP.Net MVC</title>
		<link>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/</link>
		<comments>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 07:13:12 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[DataTables]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=1041</guid>
		<description><![CDATA[Want to get jQuery DataTables to work in an ASP.Net MVC project ??? Here is a demo project in C# that should get you started.  If I find the time, I&#8217;ll post details on how this was put together (but I think most developers will benefit more from running and stepping through the source code [...]]]></description>
			<content:encoded><![CDATA[<p>Want to get jQuery <a href="http://www.datatables.net/">DataTables</a> to work in an ASP.Net MVC project ???</p>
<p>Here is a <a href="http://techencoder.com/wp-content/uploads/2009/12/ASP.Net-MVC-and-DataTables-Demo.zip">demo project</a> in C# that should get you started.  If I find the time, I&#8217;ll post details on how this was put together (but I think most developers will benefit more from running and stepping through the source code anyway).</p>
<p>Hope this helps someone.  Happy Programming!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewing the Activity Log for VS 2010</title>
		<link>http://techencoder.com/index.php/2009/11/vs2010-activity-log/</link>
		<comments>http://techencoder.com/index.php/2009/11/vs2010-activity-log/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 20:41:07 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=918</guid>
		<description><![CDATA[Are you repeatedly getting a less than helpful error dialog in Visual Studio 2010? Go to the command line and run devenv.exe with logging, reproduce the error in the IDE and then open ActivityLog.xml: C:\&#62;cd &#34;C:\Program Files &#40;x86&#41;\Microsoft Visual Studio 10.0\Common7\IDE&#34; C:\Program Files &#40;x86&#41;\Microsoft Visual Studio 10.0\Common7\IDE&#62;devenv /Log C:\Program Files &#40;x86&#41;\Microsoft Visual Studio 10.0\Common7\IDE&#62;cd %APPDATA% [...]]]></description>
			<content:encoded><![CDATA[<p>Are you repeatedly getting a less than helpful error dialog in Visual Studio 2010?</p>
<p>Go to the command line and run <a title="devenv.exe logging" href="http://msdn.microsoft.com/en-us/library/ms241272(VS.100,lightweight).aspx">devenv.exe with logging</a>, reproduce the error in the IDE and then open ActivityLog.xml:</p>
<p><img class="aligncenter size-full wp-image-919" title="devenv" src="http://techencoder.com/wp-content/uploads/2009/10/devenv.png" alt="devenv" width="600" height="174" /></p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\&gt;cd &quot;C:\Program Files <span style="color: #66cc66;">&#40;</span>x86<span style="color: #66cc66;">&#41;</span>\Microsoft Visual Studio <span style="color: #cc66cc;">10.0</span>\Common7\IDE&quot;
C:\Program Files <span style="color: #66cc66;">&#40;</span>x86<span style="color: #66cc66;">&#41;</span>\Microsoft Visual Studio <span style="color: #cc66cc;">10.0</span>\Common7\IDE&gt;devenv /Log
C:\Program Files <span style="color: #66cc66;">&#40;</span>x86<span style="color: #66cc66;">&#41;</span>\Microsoft Visual Studio <span style="color: #cc66cc;">10.0</span>\Common7\IDE&gt;cd <span style="color: #33cc33;">%</span><span style="color: #448888;">APPDATA</span><span style="color: #33cc33;">%</span>
C:\Documents and Settings\username\Application Data&gt;cd &quot;Microsoft\VisualStudio\<span style="color: #cc66cc;">10.0</span>&quot;
C:\Documents and Settings\username\Application Data\Microsoft\VisualStudio\<span style="color: #cc66cc;">10.0</span>&gt;dir ActivityLog.xml /B ActivityLog.xml
C:\Documents and Settings\username\Application Data\Microsoft\VisualStudio\<span style="color: #cc66cc;">10.0</span>&gt;notepad ActivityLog.xml</pre></div></div>

<p>Look for entries with type=Error, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;entry&gt;
&lt;record&gt;263&lt;/record&gt;
&lt;time&gt;2009/10/24 22:50:09.218&lt;/time&gt;
&lt;type&gt;Error&lt;/type&gt;
&lt;source&gt;Editor or Editor Extension&lt;/source&gt;
&lt;description&gt;System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.VisualStudio.Editor.Implementation.VsFontsAndColorsInformation.GetEditorFontForItem(IVsColorTable colorTable, Int32 itemIndex, FONTCOLORPREFERENCES2 fontColorPreferences)
at Microsoft.VisualStudio.Editor.Implementation.VsFontsAndColorsInformation.TryGetItemValue(String itemKey, ResourceDictionary&amp; itemValue)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMap.CreateResourceDictionaryFromProvision(String key)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMap.GetProperties(String key)
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMap.GetDefaultProperties()
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMap..ctor(ICollection`1 formats, IClassificationTypeRegistryService classificationTypeRegistry, IEditorFormatMap formatMap)
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMapService.GetClassificationFormatMap(String category)
at Microsoft.VisualStudio.Text.Classification.Implementation.ViewSpecificFormatMap.AttachToFormatMaps()
at Microsoft.VisualStudio.Text.Classification.Implementation.ViewSpecificFormatMap..ctor(IClassificationFormatMapService classificationFormatMapService, IEditorFormatMapService editorFormatMapService, ITextView textView)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMapService.&lt;&gt;c__DisplayClass1.&lt;GetEditorFormatMap&gt;b__0()
at Microsoft.VisualStudio.Utilities.PropertyCollection.GetOrCreateSingletonProperty[T](Object key, Func`1 creator)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMapService.GetEditorFormatMap(ITextView textView)
at Microsoft.VisualStudio.Text.Editor.Implementation.OutliningAdornmentManagerFactory.CreateTagger[T](ITextView textView, ITextBuffer buffer)
at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer)&lt;/description&gt;
&lt;/entry&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/11/vs2010-activity-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Castle Windsor 2.0 in ASP.Net MVC 1.0</title>
		<link>http://techencoder.com/index.php/2009/10/castle-windsor-and-aspnet-mvc/</link>
		<comments>http://techencoder.com/index.php/2009/10/castle-windsor-and-aspnet-mvc/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 05:26:33 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=670</guid>
		<description><![CDATA[Consider yourself warned:  I&#8217;m just getting started with ASP.Net MVC, Dependency Injection (DI) and Inversion of Control (IoC), so this post is more for my benefit than yours. ASP.Net MVC 1.0 ASP.Net MVC 1.0 was released in March 2009. Read this tutorial to start learning it. Note that ASP.Net MVC is bin deployable which is [...]]]></description>
			<content:encoded><![CDATA[<p>Consider yourself warned:  I&#8217;m just getting started with ASP.Net MVC, Dependency Injection (DI) and Inversion of Control (IoC), so this post is <a title="Why blog? The best way to learn something is to teach it." href="http://stackoverflow.com/questions/745570/how-can-social-networking-sites-make-you-a-better-developer/745703#745703">more for my benefit than yours</a>.</p>
<p><img class="aligncenter size-full wp-image-698" title="warning-castle-windsor" src="http://techencoder.com/wp-content/uploads/2009/06/warning-castle-windsor.jpg" alt="warning-castle-windsor" width="600" height="158" /></p>
<h3>ASP.Net MVC 1.0</h3>
<p><a title="Download ASP.Net MVC" href="http://msdn.microsoft.com/en-us/library/dd394709.aspx">ASP.Net MVC 1.0</a> was released in March 2009. Read <a title="ASP.Net MVC Tutorial" href="http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx">this tutorial</a> to start learning it. Note that ASP.Net MVC is <a title="bin deploy ASP.Net MVC" href="http://haacked.com/archive/2008/11/03/bin-deploy-aspnetmvc.aspx">bin deployable</a> which is very nice if you don&#8217;t want to install anything on the production web server.</p>
<h3>Inversion of Control and Castle Windsor</h3>
<p>Castle Windsor 2.0 was just released this Summer after <a title="Castle Windsor 2.0 RTM Released" href="http://ayende.com/Blog/archive/2009/05/05/castle-windsor-2.0-rtm-released.aspx">a very long development period</a>.  To start using it, <a href="http://www.castleproject.org/castle/download.html">go get the dlls</a> and reference them in your project.</p>
<p>If you are completely new to the IoC pattern, go <a href="http://msdn.microsoft.com/en-us/magazine/cc337885.aspx">read James Kovacs&#8217; article</a> and <a title="DimeCasts: Castle Windsor series" href="http://www.dimecasts.net/Casts/ByTag/Windsor%20Castle">watch these videos</a>.  They will get a typical .Net programmer up to speed pretty fast.  You should also go to <a href="http://martinfowler.com/articles/injection.html">the canonical source for IoC</a>, but frankly I learned more from the other links and I think you will too.</p>
<p>To learn how to configure the Windsor container in your project, <a title="Castle Windsor Documentation" href="http://www.castleproject.org/container/documentation/trunk/index.html">read the project&#8217;s documentation</a> and continue with this article for an example in ASP.Net MVC.</p>
<h3>Putting them Together</h3>
<p><img class="aligncenter size-full wp-image-696" title="castle-plus-aspnet-mvc" src="http://techencoder.com/wp-content/uploads/2009/06/castle-plus-aspnet-mvc.png" alt="castle-plus-aspnet-mvc" width="600" height="64" /></p>
<p>Getting Windsor to work in an ASP.Net MVC application is easy if you know what to do.  Most things are that way, huh?</p>
<p>For a bare bones integration, just follow these steps:</p>
<ol>
<li>Install ASP.NET MVC 1.0.  The <a title="Microsoft Web Platform Installer" href="http://www.microsoft.com/web/Downloads/platform.aspx">Web Platform Installer</a> is great for this and a ton of other projects.  Try it.  You&#8217;ll be glad you did.</li>
<li>Open VS 2008,  go to File-&gt;New-&gt;Project&#8230; and select the &#8220;<em>ASP.NET MVC Web Application</em>&#8221; project template.</li>
<li>Select &#8220;No&#8221; on the proceeding dialog.  Do not create a unit test project.</li>
<li><a title="Castle Windsor download page" href="http://www.castleproject.org/castle/download.html">Download</a> Castle Windsor 2.0</li>
<li>Copy the dlls (<em>Castle.DynamicProxy2, Castle.MicroKernel, Castle.Windsor and Castle.Core</em>) to your solution&#8217;s folder or a sub folder.</li>
<li>Add them as references to the web project. (Right-click the &#8220;References&#8221; folder, select &#8220;Add Reference&#8230;&#8221;, select the &#8220;Browse&#8221; tab and navigate to the dlls to add them.)</li>
<li>Add a new XML file to the web project.  Name it &#8220;windsor.config&#8221; and enter the following:</li>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--Note: Since this is a web application, all components must have a lifesytle of 'transient' or 'preWebRequest' --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;components<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!--This is where we could declaratively add components to the Windsor container if we wanted to.--&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/components<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<li>Create an interface IMessageHolder and a class MessageHolder.  (We will use this code to illustrate injecting an instance of MessageHolder into the HomeController.)</li>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IMessageHolder
<span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">string</span> GetMessage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MessageHolder <span style="color: #008000;">:</span> IMessageHolder
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080;">#region IMessageHolder Members</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetMessage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;Welcome to ASP.NET MVC using Castle Windsor 2.0!&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<li>Create a controller factory for Windsor:</li>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WindsorControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IWindsorContainer _container<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WindsorControllerFactory<span style="color: #000000;">&#40;</span>IWindsorContainer container<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_container <span style="color: #008000;">=</span> container<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> IController GetControllerInstance<span style="color: #000000;">&#40;</span>Type controllerType<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>controllerType <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// Force an exception message from the base class.</span>
			<span style="color: #008080; font-style: italic;">// This will be more informative than what we would get otherwise,</span>
			<span style="color: #008080; font-style: italic;">// see: http://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp/721313#721313</span>
			<span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetControllerInstance</span><span style="color: #000000;">&#40;</span>controllerType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #008080; font-style: italic;">// Exception here ????</span>
		<span style="color: #008080; font-style: italic;">// Check the windsor configuration.</span>
		<span style="color: #008080; font-style: italic;">// Make sure that Windsor can match the component to load.</span>
		<span style="color: #0600FF;">return</span> _container.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span>controllerType<span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> IController<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ReleaseController<span style="color: #000000;">&#40;</span>IController controller<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_container.<span style="color: #0000FF;">Release</span><span style="color: #000000;">&#40;</span>controller<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<li>Open Global.asax.cs and replace it with the following:</li>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MvcApplication <span style="color: #008000;">:</span> HttpApplication
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;</span>
	<span style="color: #008080; font-style: italic;">/// This has to be static, because more than one instance</span>
	<span style="color: #008080; font-style: italic;">/// of the HttpApplication can potentially be created.</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IWindsorContainer _container<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RegisterRoutes<span style="color: #000000;">&#40;</span>RouteCollection routes<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		routes.<span style="color: #0000FF;">IgnoreRoute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		routes.<span style="color: #0000FF;">MapRoute</span><span style="color: #000000;">&#40;</span>
			<span style="color: #666666;">&quot;Default&quot;</span>, <span style="color: #008080; font-style: italic;">// Route name</span>
			<span style="color: #666666;">&quot;{controller}/{action}/{id}&quot;</span>, <span style="color: #008080; font-style: italic;">// URL with parameters</span>
			<span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>controller <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Home&quot;</span>, action <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Index&quot;</span>, id <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// Parameter defaults</span>
			<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		InitializeWindsor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Setup the MVC framework to use our Windsor controller factory.</span>
		ControllerBuilder.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">SetControllerFactory</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WindsorControllerFactory<span style="color: #000000;">&#40;</span>_container<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> InitializeWindsor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_container <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			_container <span style="color: #008000;">=</span> Build<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;windsor.config&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IWindsorContainer Build<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> configPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var resource <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileResource<span style="color: #000000;">&#40;</span>configPath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		var interperter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlInterpreter<span style="color: #000000;">&#40;</span>resource<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		var container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span>interperter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Register controllers</span>
		container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>AllTypes
					   .<span style="color: #0000FF;">Of</span><span style="color: #008000;">&lt;</span>Controller<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
					   .<span style="color: #0000FF;">FromAssembly</span><span style="color: #000000;">&#40;</span>Assembly.<span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">/* Modify the name with which the component is registered
		See: http://blog.andreloker.de/post/2009/03/28/ASPNET-MVC-with-Windsor-programmatic-controller-registration.aspx */</span>
					   .<span style="color: #0000FF;">Configure</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">LifeStyle</span>.<span style="color: #0000FF;">Transient</span>.<span style="color: #0000FF;">Named</span><span style="color: #000000;">&#40;</span>c.<span style="color: #0000FF;">Implementation</span>.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Register all other types.</span>
		container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>
			Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IMessageHolder<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>MessageHolder<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">LifeStyle</span>.<span style="color: #0000FF;">Transient</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> container<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<li>Add a constructor to the HomeController and give it an IMessageHolder parameter.</li>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #000000;">&#91;</span>HandleError<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> Controller
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IMessageHolder _holder<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> HomeController<span style="color: #000000;">&#40;</span>IMessageHolder holder<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _holder <span style="color: #008000;">=</span> holder<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> _holder.<span style="color: #0000FF;">GetMessage</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> ActionResult About<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Since IMessageHolder is registered with Windsor, the container will resolve IMessageHolder to an instance of MessageHolder at run-time.  This is called <a href="http://en.wikipedia.org/wiki/Dependency_injection">dependency injection</a> because the HomeController dependency is injected through the constructor.  Of course, you could do this without an IoC container such as Windsor, but in larger (real world) applications the container will greatly simplify the process of managing dependencies, which means you have a better chance of keeping things <a href="http://en.wikipedia.org/wiki/Loose_coupling">loosely coupled</a> and falling into <a href="http://www.codinghorror.com/blog/archives/000940.html">the pit of success</a>.
</p>
<p>
Notice also that we display the message in Index() to verify that the Windsor container is giving us a valid instance of the type registered for IMessageHolder.  This is just a simple way to see the result of what occurred.
</p>
<li>Run the application and you should see the message on the home page.  Click <a href='http://techencoder.com/wp-content/uploads/2009/10/mvcwebapplication1.zip'>here</a> to download the sample project.</li>
<p><img src="http://techencoder.com/wp-content/uploads/2009/10/aspnet-and-windsor-integration.png" alt="aspnet-and-windsor-integration" title="aspnet-and-windsor-integration" width="600" height="404" class="aligncenter size-full wp-image-956" />
</ol>
<p>
<a href='http://techencoder.com/wp-content/uploads/2009/10/mvcwebapplication1.zip'>Project Source Code</a> for this post.
</p>
<p>I hope that this walk-through helps someone get started with Windsor.  It really is an incredible project to learn and if used correctly it will make your programs much better.  Have fun with it and Happy Programming!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/10/castle-windsor-and-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>LINQ-To-SQL DataContext and NullReferenceException</title>
		<link>http://techencoder.com/index.php/2009/06/linq-to-sql-datacontext-and-nullreferenceexception/</link>
		<comments>http://techencoder.com/index.php/2009/06/linq-to-sql-datacontext-and-nullreferenceexception/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 16:57:41 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=763</guid>
		<description><![CDATA[LINQ classes generated by the VS designer (or sqlmetal.exe) are marked partial by default. &#91;System.Data.Linq.Mapping.DatabaseAttribute&#40;Name=&#34;MyData&#34;&#41;&#93; public partial class MyDataDataContext : System.Data.Linq.DataContext &#123; // blah ... blah ... &#125; public partial class MyDataDataContext &#123; // My additional methods, constructors, etc. &#125; As I&#8217;ve shown above, this allows you to extend a class without modifying the original [...]]]></description>
			<content:encoded><![CDATA[<p>LINQ classes generated by the VS designer (or <a title="Code Generation Tool (SqlMetal.exe)" href="http://msdn.microsoft.com/en-us/library/bb386987.aspx">sqlmetal.exe</a>) are marked partial by default.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Data</span></span>.<span style="color: #0000FF;">Linq</span>.<span style="color: #0000FF;">Mapping</span>.<span style="color: #0000FF;">DatabaseAttribute</span><span style="color: #000000;">&#40;</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MyData&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataDataContext <span style="color: #008000;">:</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Data</span></span>.<span style="color: #0000FF;">Linq</span>.<span style="color: #0000FF;">DataContext</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// blah ... blah ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataDataContext
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// My additional methods, constructors, etc.</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As I&#8217;ve shown above, this allows you to extend a class without modifying the original code.  The <a title="Partial Class Definitions at MSDN" href="http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx">additional class definitions</a> just live somewhere else, typically in another file, and the compiler integrates them as needed.  That&#8217;s great for keeping your code out of the machine generated file, but you still need to be aware of what the machine generated code is doing before you extend it!  One thing to check is the default (sqlmetal generated) constructor when you are planning to overload it with your own. <strong>In some cases, the default constructor will initialize dependencies, so if you write an overload, make sure it calls the default constructor too:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataDataContext
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> MyDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// We must call the designer generated constructor because it contains initialization code.</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// blah ... blah ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice how I used the constructor initialization keyword <code>: this()</code>.<br />
From the C# Specification <a href="http://msdn.microsoft.com/en-us/library/aa645603(VS.71).aspx">Section 10.10.1</a></p>
<div style="padding-left:30px; color:Chocolate"><strong>&#8220;An instance constructor initializer of the form this(argument-listopt) causes an instance constructor from the class itself to be invoked.&#8221;</strong></div>
<div style="padding-top:10px">If your DataContext is throwing a NullReferenceException <em>&#8220;Object reference not set to an instance of an object&#8221;</em>,  make sure you are calling the default constructor.</div>
<div id="attachment_764" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-764" title="null-reference-exception" src="http://techencoder.com/wp-content/uploads/2009/06/null-reference-exception.png" alt="Don't forget to call the default constructor!" width="600" height="255" /><p class="wp-caption-text">Don&#39;t forget to call the default constructor!</p></div>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/06/linq-to-sql-datacontext-and-nullreferenceexception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying Silverlight 2 on IIS</title>
		<link>http://techencoder.com/index.php/2009/05/deploying-silverlight-2-on-iis/</link>
		<comments>http://techencoder.com/index.php/2009/05/deploying-silverlight-2-on-iis/#comments</comments>
		<pubDate>Fri, 15 May 2009 04:38:46 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[MIME Types]]></category>
		<category><![CDATA[Silverlight 2]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=328</guid>
		<description><![CDATA[Silverlight 2 deployment on IIS 5.1 or 6 requires the addition of 2 MIME types in the web server&#8217;s configuration: .xap application/x-silverlight-app .xaml application/xaml+xml Adding them to IIS is easy: Right click on the website and select &#8220;Properties&#8221; Choose the HTTP Headers tab. Click the &#8220;File Types&#8230;&#8221; button. (It is labeled &#8220;Mime Types&#8230;&#8221; in IIS [...]]]></description>
			<content:encoded><![CDATA[<p>Silverlight 2 deployment on IIS 5.1 or 6 requires the addition of 2 MIME types in the web server&#8217;s configuration:</p>
<ul>
<li><code>.xap application/x-silverlight-app</code></li>
<li><code>.xaml application/xaml+xml</code></li>
</ul>
<p>Adding them to IIS is easy:</p>
<ol>
<li>Right click on the website and select &#8220;Properties&#8221;<br />
<img class="alignnone size-full wp-image-342" title="iis-default-website-properties" src="http://techencoder.com/wp-content/uploads/2009/02/iis-default-website-properties.png" alt="iis-default-website-properties" width="585" height="483" /></li>
<li>Choose the HTTP Headers t<span>ab.<br />
<img class="alignnone size-full wp-image-340" title="iss-mime-mapping" src="http://techencoder.com/wp-content/uploads/2009/02/iss-mime-mapping.png" alt="iss-mime-mapping" width="472" height="465" /><br />
</span></li>
<li><span>Click the &#8220;File Types&#8230;&#8221; button.<br />
(It is labeled &#8220;Mime Types&#8230;&#8221; in IIS v6.0)<br />
<img class="alignnone size-full wp-image-341" title="iss-mime-types" src="http://techencoder.com/wp-content/uploads/2009/02/iss-mime-types.png" alt="iss-mime-types" width="347" height="377" /><br />
</span></li>
<li><span>Add the types.<br />
<img class="alignnone size-full wp-image-338" title="adding-xap-mime-type" src="http://techencoder.com/wp-content/uploads/2009/02/adding-xap-mime-type.png" alt="adding-xap-mime-type" width="347" height="165" /> <img class="alignnone size-full wp-image-337" title="adding-xaml-mime-type" src="http://techencoder.com/wp-content/uploads/2009/02/adding-xaml-mime-type.png" alt="adding-xaml-mime-type" width="347" height="165" /> </span></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/05/deploying-silverlight-2-on-iis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accelerating Future, Part 2</title>
		<link>http://techencoder.com/index.php/2009/05/accelerating-future-part-2/</link>
		<comments>http://techencoder.com/index.php/2009/05/accelerating-future-part-2/#comments</comments>
		<pubDate>Tue, 05 May 2009 04:22:46 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Futurism]]></category>
		<category><![CDATA[Accelerating Change]]></category>
		<category><![CDATA[biotechnology]]></category>
		<category><![CDATA[human genome project]]></category>
		<category><![CDATA[moore's law]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=387</guid>
		<description><![CDATA[In part 1 of this series, I explained that exponential progress in computing has been going on for a long time.  Gordon Moore saw the trend over 45 years ago, and industry experts believe it will continue for at least another decade or even much longer than that. I also made the case that this [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">In <a title="Accelerating Future, Part 1" href="http://techencoder.com/index.php/2009/03/accelerating-future-part-1/" target="_self">part 1</a> of this series, I explained that exponential progress in computing has been going on for a long time.  Gordon Moore saw the trend over 45 years ago, and industry experts believe it will continue for <a title="Moore's Law through 2018" href="http://www.sciencedaily.com/releases/2008/06/080606152512.htm" target="_self">at least another decade</a> or <a title="Moore's Law to hold at least through 2029" href="http://www.h-online.com/news/IDF-Intel-says-Moore-s-Law-holds-until-2029--/110469" target="_self">even much longer</a> than that.</p>
<p>I also made the case that <a title="Do you know? video" href="http://www.youtube.com/watch?v=jpEnFwiqdx8" target="_self">this trend is not limited to computing</a>. Progress in other areas has driven the worldwide average standard of living <a title="The Worldwide Standard of Living" href="http://ideas.repec.org/a/aea/jecper/v14y2000i1p7-26.html" target="_self">much higher</a> today than just 50 or 100 years ago.  <a title="The State of Humanity: Steadily Improving" href="http://www.cato.org/pubs/policy_report/pr-so-js.html">Humanity is better off [year after year] in just about every measurable material way</a>.</p>
<p><a href="http://www.flickr.com/photos/markkelley/1022720488/"></a></p>
<div id="attachment_627" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.flickr.com/photos/markkelley/1022720488/"><img class="size-full wp-image-627" title="maasai" src="http://techencoder.com/wp-content/uploads/2009/05/maasai.jpg" alt="Maasi using modern technology.  Used with permission by MarkKelley." width="600" height="300" /></a><p class="wp-caption-text">Modern technology in the 3rd world.  Used with permission by MarkKelley.</p></div>
<p>We are at a significant point in history. Great advances in computing, nanotech, biotech, robotics, energy, artificial intelligence and communications are occurring every day.  Most do not make the mainstream news, but their cumulative effect will raise our standard of living more rapidly than ever before.  I do not predict a completely Utopian future (we will discuss the increased dangers in a subsequent post), but I do believe <strong>most people will have the chance to live in far greater prosperity if we avoid the biggest risks.</strong></p>
<p>Beginning with this post, I want to point out real world examples of accelerating change.  I think you will see that Moore&#8217;s Law is not just an isolated trend confined to the world of geeks.  It is something that will affect your life on multiple fronts, regardless of who you are.</p>
<hr />
<div id="attachment_474" class="wp-caption alignright" style="width: 179px"><a href="http://www.flickr.com/photos/johnjobby/1971970184/"><img class="size-full wp-image-474" title="Human genome printed, Wellcome Trust" src="http://techencoder.com/wp-content/uploads/2009/03/human-genome-printed.jpg" alt="Human genome printed, Wellcome Trust" width="169" height="400" /></a><p class="wp-caption-text">Human genome printed, used with permission by JohnJobby</p></div>
<blockquote><p>The fusion of health care and information technology – i.e. biotechnology – is experiencing exponential growth.</p></blockquote>
<p>The fusion of health care and information technology – i.e. <strong>biotechnology</strong> – is one of many industries experiencing rapid and (by some measures) exponential growth.</p>
<p>Biotech could be the most promising and dangerous technology for humanity, but how soon will such things as personal DNA analysis and real time biometric monitoring become commonplace?</p>
<p>Consider the <a title="Human Genome Project on Wikipedia" href="http://en.wikipedia.org/wiki/Human_genome_project">Human Genome Project</a> (1990-2003):   One billion DNA base pairs were sequenced in the first 4 <strong>years</strong>.  Another billion base pairs were sequenced in the next 4 <strong>months</strong> and 1.5 billion base pairs were sequenced in the last 4 <strong>weeks</strong>. [<a title=" The Department of Energy and the Human Genome Project Fact Sheet " href="http://www.ornl.gov/sci/techresources/Human_Genome/project/whydoe.shtml" target="_self">source</a>]</p>
<p>By the end of this year, you can have your entire genome sequenced for <a title="Company Promises $5,000 genome" href="http://www.thegeneticgenealogist.com/2008/10/06/complete-genomics-will-sequence-your-entire-genome-for-5000-starting-in-2009/" target="_self">just $5000.00</a> and results will be ready in a few<em> </em><strong>days</strong>.  By 2014, genome sequencing is projected to cost <a title="The $100 Genome" href="http://www.technologyreview.com/biomedicine/20640/">less than a nice pair of jeans</a> and results will be ready in about 8 <strong>hours</strong>.</p>
<p><em>years &gt; months &gt; weeks &gt; days &gt; hours</em></p>
<p><a href="http://www.ncbi.nlm.nih.gov/Genbank/genbankstats.html">See the trend</a>?</p>
<p>The first genome cost 2.7 billion US dollars.  This price dropped to <a title="James Watson, codiscoverer of the structure of DNA, now has a copy of his very own genome." href="http://www.technologyreview.com/Biotech/18809/">$2,000,000 by 2007</a> and <a title="$1,000 Genome by the End of 2009?" href="http://www.thegeneticgenealogist.com/2008/09/26/the-1000-genome-by-the-end-of-2009/" target="_self">$1000 is coming in 2009 or 2010</a>.</p>
<p><em><span style="color: ForestGreen;"><small>$,$$$,$$$,$$$</small></span> billions &gt; <span style="color: ForestGreen;"><small>$,$$$,$$$</small></span> millions &gt; <span style="color: ForestGreen;"><small>$,$$$</small></span> thousands &gt; <span style="color: ForestGreen;"><small>$$$</small></span> hundreds</em></p>
<p><a title="DNA Sequencing Cost" href="http://singularity.com/images/charts/DNAsequencingCost.jpg">See the trend</a>?</p>
<hr />
<blockquote><p>Does the promise of a biotech revolution sound like science fiction?</p></blockquote>
<p>Personal computers (that calculate billions of instructions in one second) and personal GPS devices (that pull data from satellites 12,000 miles above the earth) would have sounded like science fiction only a few decades ago.  Does the promise of a biotech revolution sound like science fiction today?</p>
<p>I&#8217;m excited about the rapid price drop in genome sequencing, and I&#8217;m especially looking forward to the breakthroughs in medicine that DNA analysis will bring.  Once millions of people can afford this technology, we will have vast amounts of data to find new treatments on everything that ails us.  But what about the risks?  How will we deal with this information getting into the wrong hands?  How will we protect our privacy and how will we prevent genetic discrimination?  These issues have not been widely discussed, but you can bet the <a title="Nanofluidic chip could cut DNA sequencing costs dramatically" href="http://www.technologyreview.com/read_article.aspx?ch=specialsections&amp;sc=tr10&amp;id=22112">$100 genome</a> will get lots of people talking.</p>
<hr /><span style="color: Navy;"><strong>UPDATE: May 11, 2009</strong></span> &#8212; Singularity Hub is reporting <a title="Vital Signs in Remote Real-time" href="http://singularityhub.com/2009/05/11/health-cares-wireless-future-vital-signs-in-remote-real-time/">on a real time, biometric monitoring device that is now FDA approved</a>.  Niche market today&#8230; mainstream tomorrow!</p>
<p><span style="color: Navy;"><strong>UPDATE: August 10, 2009</strong></span> &#8212; Another benchmark has been reached : <a title="human-genome-completed-using-one-machine-for-four-weeks" href="http://arstechnica.com/science/news/2009/08/human-genome-completed-using-one-machine-for-four-weeks.ars">http://arstechnica.com/science/news/2009/08/human-genome-completed-using-one-machine-for-four-weeks.ars</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/05/accelerating-future-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fixing FileUpload when Visibility is Changed Asynchronously</title>
		<link>http://techencoder.com/index.php/2009/02/fixing-fileupload/</link>
		<comments>http://techencoder.com/index.php/2009/02/fixing-fileupload/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 16:16:07 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Futurism]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Asynchronous]]></category>
		<category><![CDATA[enctype]]></category>
		<category><![CDATA[FileUpload]]></category>
		<category><![CDATA[UpdatePanel]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=220</guid>
		<description><![CDATA[Somehow I always seem to find edge cases where the abstractions in ASP.Net simply don&#8217;t work.  At some point in every project, they break down and I start digging into framework-generated scripts, the DOM, or HTTP request details to figure out what is going on. Last month, I built an alternative to System.Web.UI.Timer because extending [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_283" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.flickr.com/photos/trainor/1229138273/" target="_blank"><img src="http://techencoder.com/wp-content/uploads/2009/02/upload-download.jpg" alt="Upload / Download, by johntrainor" title="upload-download" width="600" height="262" class="size-full wp-image-283" /></a><p class="wp-caption-text">Upload / Download, used with permission by johntrainor</p></div>
<p>Somehow I always seem to find edge cases where the abstractions in ASP.Net simply don&#8217;t work.  At some point in every project, <a title="Leaky Abstractions" href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html" target="_blank">they break down</a> and I start digging into framework-generated scripts, the DOM, or HTTP request details to figure out what is going on.</p>
<p>Last month, I built an <a title="JavaScript and Timer OnTick" href="http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/" target="_blank">alternative to System.Web.UI.Timer</a> because extending it with client side events would have been much more complicated than a plain JavaScript implementation.  This month, I spent almost an entire day finding out why a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx" title="FileUpload Documentation on MSDN" target="_blank">FileUpload</a> would sometimes fail to send data back to the server.  The problem arises whenever visibility is changed during an async postback:</p>
<p><strong>The Problem:</strong></p>
<p>Suppose you have a FileUpload inside an UpdatePanel.  It is initially set invisible because the user is supposed to do something before seeing it.  In this example, a button click asynchronously posts back to the server and the server makes the FileUpload visible.  The page would look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Initially set the FileUpload invivible.</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Page.<span style="color: #0000FF;">IsPostBack</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> showButton_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// User has requested the FileUpload.  Make it visible.</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> submitButton_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Did the FileUpload send data back to the server?  </span>
        <span style="color: #008080; font-style: italic;">// Print out true or false.</span>
        statusLabel.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> 
            <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;File was found = {0}&quot;</span>, fileUpload.<span style="color: #0000FF;">HasFile</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>UpdatePanel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;updatePanel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> UpdateMode<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Conditional&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>ContentTemplate<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Show FileUpload&quot;</span> 
            OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Panel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;fileUploadPanel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>FileUpload ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;fileUpload&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Submit&quot;</span> 
                OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Panel<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;statusLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>ContentTemplate<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>Triggers<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>AsyncPostBackTrigger ControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>PostBackTrigger ControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>Triggers<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>UpdatePanel<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>The problem with this implementation is that <strong>the FileUpload fails to send a file on the first submission after it is made visible.</strong> Subsequent submissions work fine, but we need a solution that always works.</p>
<p><strong>The First Solution (not great):</strong></p>
<p>Several programmers <a title="FileUpload Failure Question1" href="http://forums.asp.net/t/1336476.aspx" target="_blank">have</a> <a title="FileUpload Failure Question2" href="http://forums.asp.net/t/1060363.aspx" target="_blank">noticed</a> that setting the visibility false <em>on the server</em> can cause this problem, so an easy fix is to toggle visibility client side:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// fails</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Style</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;display&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;none&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// works</span></pre></td></tr></table></div>

<p>Using <code>.Style["display"]</code> in lieu of <code>.Visible</code> is fine, but now the server sends the FileUpload to the browser with <em>every request</em>.  In some cases, that is not what you want.  You might want to reduce unnecessary data transfer or you might have client scripts that assume the control does not exist when it is not visible, etc.  This solution is also problematic in that you can apply the fix without knowing <em>why</em> it works.</p>
<p><strong>The Second Solution (much better):</strong></p>
<p>Going back to run our original markup, an examination of the DOM using <a href="http://getfirebug.com/" target="_blank" title="Get FireBug for FireFox">Firebug</a> or <a href="http://en.wikipedia.org/wiki/IE_Developer_Toolbar" target="_blank" title="Internet Explorer Developer Toolbar">IE Developer Toolbar</a> reveals that there is a difference in the form tag whenever the post fails or succeeds.  It was not obvious at first glance, but using <a href="http://en.wikipedia.org/wiki/WinMerge" title="WinMerge: An open source file comparison tool" target="_blank">WinMerge</a> helped to point it out.</p>
<p>DOM markup before a failed POST  (first attempt):<br />
<img src="http://techencoder.com/wp-content/uploads/2009/02/before-failure1.png" alt="Markup before a failed file upload" title="Markup before a failed file upload" width="594" height="56" class="aligncenter size-full wp-image-238" /></p>
<p>DOM markup before a successful POST (subsequent attempts):<br />
<img src="http://techencoder.com/wp-content/uploads/2009/02/before-success.png" alt="Markup before a successful file upload" title="Markup before a successful file upload" width="594" height="75" class="aligncenter size-full wp-image-239" /></p>
<p>Apparently, the ASP.Net runtime adds <a title="Enctype Attribute Information" href="http://www.htmlcodetutorial.com/forms/_FORM_ENCTYPE.html" target="_blank"><code>enctype="multipart/form-data"</code></a> to the form tag whenever it recognizes a FileUpload (or <a title="Input Type File Information" href="http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_FILE.html" target="_blank">other file input</a>) is being sent to the browser.  Setting the visibility false during initial page load prevents this, and the runtime no longer adds the attribute.  </p>
<p>Using <code>Style["display"] = "none"</code> &#8212; our first solution &#8212; fixes the problem in a roundabout way:  It causes the FileUpload to be sent to the browser on initial page load which in turn causes the runtime to add an enctype.  The presence of <a href="http://www.htmlcodetutorial.com/forms/_FORM_ENCTYPE.html" target="_blank" title="building a simple html file upload">enctype is really what we need</a>, but since showButton is an AsyncPostBackTrigger, the form tag (being outside the UpdatePanel) is not updated when showButton is clicked.  You have to manually add <a href="http://www.htmlcodetutorial.com/forms/_FORM_ENCTYPE.html" target="_blank" title="html enctype information">enctype</a> at some point before the FileUpload is made visible on the page.</p>
<p>There are several ways to ensure that this attribute is added to the form.  Anyone reading this far into the post will probably know how to do that, but here is a working example just for closure.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Initially set the FileUpload invivible.</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Page.<span style="color: #0000FF;">IsPostBack</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> showButton_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// User has requested the FileUpload.  Make it visible.</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">fileUploadPanel</span>.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> submitButton_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Did the FileUpload send data back to the server?  </span>
        <span style="color: #008080; font-style: italic;">// Print out true or false.</span>
        statusLabel.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> 
            <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;File was found = {0}&quot;</span>, fileUpload.<span style="color: #0000FF;">HasFile</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ENCTYPE<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MULTIPART/FORM-DATA&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>UpdatePanel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;updatePanel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> UpdateMode<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Conditional&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>ContentTemplate<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Show FileUpload&quot;</span> 
            OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Panel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;fileUploadPanel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>FileUpload ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;fileUpload&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Submit&quot;</span> 
                OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Panel<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;statusLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>ContentTemplate<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>Triggers<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>AsyncPostBackTrigger ControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;showButton&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>PostBackTrigger ControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submitButton&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>Triggers<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>UpdatePanel<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/02/fixing-fileupload/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Keeping Up With the Programmers</title>
		<link>http://techencoder.com/index.php/2009/02/keeping-up/</link>
		<comments>http://techencoder.com/index.php/2009/02/keeping-up/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 04:38:59 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Futurism]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Keeping Up]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Progress]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=42</guid>
		<description><![CDATA[Staying current in the world of .Net is an enormous task. Framework version 1 (polished in 1.1) introduced the CLR, C#, VB.Net and ASP.Net.   Version 2.0 brought generics, partial classes, anonymous methods, themes, skins, webparts and master pages.  Version 3.0 added WPF, WCF, WF and CardSpace.  Version 3.5 added LINQ, XML literals, lambda expressions, [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_47" class="wp-caption aligncenter" style="width: 610px"><a title="Keeping up with the Joneses" href="http://en.wikipedia.org/wiki/Keeping_up_with_the_joneses" target="_blank"><strong><strong><img class="size-full wp-image-47" title="keeping-up-with-the-joneses" src="http://techencoder.com/wp-content/uploads/2009/01/keeping-up-with-the-joneses.png" alt="Keeping up with the Joneses, By Pop Momand, 1920" width="600" height="260" /></strong></strong></a><p class="wp-caption-text">Keeping up with the Joneses, By Pop Momand, 1920.  Modified By R.Claypool, 2009.</p></div>
<p><strong>Staying current in the world of .Net is an enormous task.</strong> Framework version 1 (polished in 1.1) introduced the CLR, C#, VB.Net and ASP.Net.   Version 2.0 brought generics, partial classes, anonymous methods, themes, skins, webparts and master pages.  Version 3.0 added WPF, WCF, WF and CardSpace.  Version 3.5 added LINQ, XML literals, lambda expressions, type inference, anonymous types, extension methods, automatic properties and object initializers.  </p>
<p>During this time, we have also had 2 major releases of SQL Server, Silverlight 1.0, Silverlight 2 and several versions of Visual Studio:</p>
<p>
<script type="text/javascript">
    //<![CDATA[
    var tl;

    window.onload = function() { setTimeout("onLoad();", 1000) };
    function onLoad() {
        var eventSource = new Timeline.DefaultEventSource();
        var bandInfos = [
     Timeline.createBandInfo({
         eventSource: eventSource,
         date: "Oct 01 2005 00:00:00 GMT",
         width: "100%",
         intervalUnit: Timeline.DateTime.YEAR,
         intervalPixels: 55
     })
   ];
        tl = Timeline.create(document.getElementById("timeline1"), bandInfos);
        Timeline.loadXML("http://techencoder.com/microsoft.xml", function(xml, url) { eventSource.loadXML(xml, url); });
    }

    var resizeTimerID = null;
    window.onresize = function() { setTimeout("onResize();", 1000) };
    function onResize() {
        if (resizeTimerID == null) {
            resizeTimerID = window.setTimeout(function() {
                resizeTimerID = null;
                tl.layout();
            }, 500);
        }
    }
    //]]&gt;
</script>

<div id="timeline1-area" class="wp-caption aligncenter">
<div id="timeline1" style="height: 210; border: 1px solid #aaa"; width: 600px;>
<span>
<br />
    This page uses JavaScript to display a Timeline. 
    <br />
    Please enable JavaScript in your browser to see the Timeline if it does not show.
<br />
</span>
</div>
<p id="timeline1-caption" class="wp-caption-text">
Timeline of Microsoft Developer Products
</p>
</div>
</p>
<p><strong>If that is not enough, notice that this pace of change is not slowing down!</strong> <br />In fact, the next 12 months will bring more changes to .Net programming than ever before:</p>
<ul>
<li>C# 4.0 is adding <a href="http://blogs.msdn.com/ericwhite/archive/2008/10/29/anders-hejlsberg-introduces-c-4-0-at-pdc-2008.aspx">covariance and contravariance, dynamic typing and optional named parameters</a>.</li>
<li>F# is becoming a <a href="http://blogs.msdn.com/somasegar/archive/2007/10/17/f-a-functional-programming-language.aspx">first class language</a>.</li>
<li>Framework 4.0 will get another <a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx">batch</a> <a href="http://developers.de/blogs/damir_dobric/archive/2008/10/05/what-is-new-in-net-4-0.aspx">of</a> <a href="http://blogs.msdn.com/wenlong/archive/2008/09/07/net-4-0-wf-wcf-and-oslo.aspx">updates</a> and <a href="http://blogs.msdn.com/pfxteam/archive/2008/10/10/8994927.aspx">extensions</a>.</li>
<li>Silverlight 3 will have enough <a href="http://silverlighthack.com/post/2008/12/11/Silverlight-3-What-we-Know-So-Far-What-We-Can-Predict-(Part-1-of-2).aspx">new features</a> to surpass Adobe Flash (IMO).</li>
<li>ASP.Net MVC + jQuery will provide a <a href="http://stackoverflow.com/questions/46031/why-does-the-aspnet-web-forms-model-suck">much needed alternative</a> to ASP.Net WebForms.</li>
<li>Windows Asure will bring <a href="http://www.davidchappell.com/blog/2008/10/introducing-azure-services-platform.html#comments">cloud computing to .Net</a>.</li>
</ul>
<div id="attachment_131" class="wp-caption alignleft" style="width: 610px"><a href="http://www.flickr.com/photos/carbonnyc/2138984229/" target="_blank"><img src="http://techencoder.com/wp-content/uploads/2009/01/tortoise-and-the-hare-in-boston.jpg" alt="Tortoise and the Hare, by CarbonNYC" title="tortoise-and-the-hare-in-boston" width="400" height="187" class="size-full wp-image-131" /></a><p class="wp-caption-text">Tortoise and the Hare, used with permission by CarbonNYC</p></div>
<p><strong>I&#8217;ve only listed a portion of the updates that are coming in 2009, so <a href="http://stackoverflow.com/questions/378724/coping-with-rapidly-changing-technology-in-particular-microsoft" target="_blank">how can we possibly keep up</a> with this constant stream change?</strong> Any small handful of these items could easily take months to learn and years to grok, so it is important to plan out the time to learn them and have some kind of reasoning behind our priorities. </p>
<p><div style="padding-left: 40px">
So &#8230; <br />What are your priorities?  <br />How did you decide they are worth your time?
</div>
</p>
<p></p>
<p>The best approach I have found is described by Mr. Jeff Atwood as a <a title="Keeping Up and &quot;Just In Time&quot; Learning" href="http://www.codinghorror.com/blog/archives/000575.html" target="_blank">just in time</a> system:</p>
<blockquote><p>I take a &#8220;Just In Time&#8221; attitude to learning new technology. I can’t possibly learn everything.  But I do try to learn enough to know what the new thing is, and when I might need it.</p></blockquote>
<p><em>&#8220;I don&#8217;t worry about keeping up with the Joneses; I focus on the specific problem at hand. I take a &#8220;Just In Time&#8221; attitude to learning new technology.  I can&#8217;t possibly learn everything. But I do try to learn enough to know what the new thing is, and when I might need it. Most of the time, <a href="http://www.codinghorror.com/blog/archives/000111.html" target="_blank">I don&#8217;t need it</a>. And when I do, I can learn it <a href="http://www.strategosinc.com/just_in_time.htm" target="_blank">Just In Time</a> to help me solve the current problem I&#8217;m working on.&#8221;</em></p>
<p>There are a ton of things I&#8217;d <em>like</em> to learn this year, but only a few that I will <em>have time</em> to learn.  Design Patterns, Silverlight, WCF, jQuery, and unit testing (probably Moq + NUnit) are my focus for 2009.  I&#8217;d love to learn F# and ASP.Net MVC, but I doubt I&#8217;ll have the time.  I also doubt that <a href="http://www.microsoft.com/azure/default.mspx" target="_blank">Asure</a>, <a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx" target="_blank">Entity Framework</a>, <a href="http://msdn.microsoft.com/en-us/magazine/cc785479.aspx" target="_blank">Prism</a>, <a href="http://code.msdn.microsoft.com/mef">MEF</a>, <a href="http://msdn.microsoft.com/en-us/devlabs/cc950525.aspx">Pex</a>, or many of the other dozens of projects out there will get <em>any</em> of my attention this year &#8212; even though they all look like useful and interesting technologies.  There is only so much that one can fit into a year!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/02/keeping-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to run JavaScript prior to System.Web.UI.Timer OnTick</title>
		<link>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/</link>
		<comments>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 07:07:34 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[StackOverflow]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[UpdatePanel]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=156</guid>
		<description><![CDATA[The short answer: you can&#8217;t &#8230; or at least not easily, but there is a better way. Let&#8217;s say you have a page that needs to post back every x minutes. In my case, I needed to implement an auto-save feature that the client requested. Saving the data would happen server side so I just [...]]]></description>
			<content:encoded><![CDATA[<p>The short answer: <em>you can&#8217;t</em> &#8230; or at least not easily, but there <em>is</em> a better way.</p>
<p>Let&#8217;s say you have a page that needs to post back every x minutes.  In my case, I needed to implement an auto-save feature that the client requested.  Saving the data would happen server side so I just used a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.timer.aspx" target="_blank">Timer</a> to initiate regular post backs.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Timer1_OnTick<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Last Postback: &quot;</span> <span style="color: #008000;">+</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Timer ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Timer1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
                OnTick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Timer1_OnTick&quot;</span> Interval<span style="color: #008000;">=</span><span style="color: #666666;">&quot;4000&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>The problem surfaced when they also wanted a client-side prompt to allow or cancel the auto post back.  <strong>System.Web.UI.Timer does not have any client side events exposed, </strong> so I searched the web and asked our friends over at StackOverflow <a href="http://stackoverflow.com/questions/439677/how-to-run-javascript-prior-to-system-web-ui-timer-ontick" target="_blank">what they would do</a>.</p>
<p>The only response to my post confirmed that the Timer is a bad fit for this situation.  I could hack it, but <em>there must be a better way.</em></p>
<div id="attachment_170" class="wp-caption alignleft" style="width: 610px"><img src="http://techencoder.com/wp-content/uploads/2009/01/square-peg-in-a-round-hole.png" alt="Square Peg in a Round Hole" title="square-peg-in-a-round-hole" width="350" height="190" class="size-full wp-image-170" /><p class="wp-caption-text">Square Peg in a Round Hole</p></div>
<p>Instead of using a Timer to initiate post backs, I switched to a combination of setTimeout() and __doPostBack().  It&#8217;s pure html and JavaScript goodness!  </p>
<p>In this example, I added an update panel to demonstrate how __doPostBack() can also be used to create a partial post back (leaving some parts of the page unchanged after the auto post back).  That is not necessary, but if you are posting back to the server automatically, an AJAX experience will probably be better for the user.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">IsPostBack</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> time <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">insidePanelLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Last Postback: &quot;</span> <span style="color: #008000;">+</span> time<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">outsidePanelLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;blah blah&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Does not show up after partial post back.</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span> language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;javascript&quot;</span><span style="color: #008000;">&gt;</span>
        window.<span style="color: #0000FF;">onload</span> <span style="color: #008000;">=</span> function<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// start the auto postback timer.</span>
            setPostbackTimer<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        function setPostbackTimer<span style="color: #000000;">&#40;</span>seconds<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            window.<span style="color: #0000FF;">setTimeout</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;conditionalPostback()&quot;</span>, seconds <span style="color: #008000;">*</span> <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        function canContinue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> confirm<span style="color: #000000;">&#40;</span><span style="color: #666666;">'The page is requesting new data.  Ok or Cancel?'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        function conditionalPostback<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>canContinue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// User permits the postback. </span>
                <span style="color: #008080; font-style: italic;">// Do it.</span>
                __doPostBack<span style="color: #000000;">&#40;</span><span style="color: #666666;">'UpdatePanel1'</span>, <span style="color: #666666;">''</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// User denied the postback. </span>
                <span style="color: #008080; font-style: italic;">// Reset the timer (they will be prompted again).</span>
                setPostbackTimer<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>UpdatePanel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;UpdatePanel1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>ContentTemplate<span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;insidePanelLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
                    Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;This text will change during auto postbacks.&quot;</span><span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>ContentTemplate<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>UpdatePanel<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;outsidePanelLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
            Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;This text will not change during auto postbacks.&quot;</span><span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p><strong>Have you ever tried to make Microsoft controls fit into a situation only to find that plain html and JavaScript are a better fit?</strong>  </p>
<p>The WebForms model often leads me to over rely on controls and I forget that plain html and JavaScript solutions are available too.  Using controls is fine, but think twice if you have to hack them to get what you want.  There is probably a better way.</p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
