<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jay Harris is Cpt. LoadTest</title>
    <link>http://www.cptloadtest.com/</link>
    <description>a .net developers blog on improving user experience of humans and coders</description>
    <language>en-us</language>
    <copyright>Jason Harris</copyright>
    <lastBuildDate>Tue, 03 Dec 2013 05:34:25 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>jharris@harrisdesigns.com</managingEditor>
    <webMaster>jharris@harrisdesigns.com</webMaster>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Azure Websites are a fantastic method of hosting your own web site. At Arana Software,
we use them often, particularly as test environments for our client projects. We can
quickly spin up a free site that is constantly up-to-date with the latest code using
continuous deployment from the project’s Git repository. Clients are able to see progress
on our development efforts without us having to worry about synchronizing codebases
or managing infrastructure. Since Windows Azure is a Microsoft offering, it is a natural
for handling .NET projects, but JavaScript-based nodejs is also a natural fit and
a first-class citizen on the Azure ecosystem.
</p>
        <h3>Incorporating Grunt
</h3>
        <p>
          <a href="http://gruntjs.com/">Grunt</a> is a JavaScript-based task runner for your
development projects to help you with those repetitive, menial, and mundane tasks
that are necessary for production readiness. This could be running unit tests, compiling
code, image processing, bundling and minification, and more. For our production deployments,
we commonly use Grunt to compile LESS into CSS, CoffeeScript into JavaScript, and
Jade into HTML, taking the code we write and preparing it for browser consumption.
We also use Grunt to optimize these various files for speed through bundling and minification.
The output of this work is done at deployment rather than development, with only the
source code committed into Git and never its optimized output.
</p>
        <h3>Git Deploy and Kudu
</h3>
        <p>
Continuous deployment will automatically update your site with the latest source code
whenever modifications are made to the source repository. This will also work with
Mercurial. There is plenty of existing documentation on <a href="http://www.windowsazure.com/en-us/develop/nodejs/common-tasks/publishing-with-git/">setting
up Git Deploy in Azure</a>, so consider that a prerequisite for this article. However,
Git Deploy, alone, will only take the files as they are in source, and directly deploy
them to the site. If you need to run additional tasks, such as compiling your .NET
source or running Grunt, that is where Kudu comes in.
</p>
        <p>
Kudu is the engine that drives Git deployments in Windows Azure. Untouched, it will
simply synchronize files from Git to your /wwwroot, but it can be easily reconfigured
to execute a deployment command, such as a Windows Command file, a Shell Script, or
a nodejs script. This is enabled through a standardized file named ".deployment".
For Grunt deployment, we are going to execute a Shell Script that will perform npm,
Bower, and Grunt commands in an effort to make our code production-ready. For other
options on .deployment, check out the <a href="https://github.com/projectkudu/kudu/wiki">Kudu
project wiki</a>.
</p>
        <p>
Kudu is also available locally for testing, and to help build out your deployment
scripts. The engine is available as a part of the cross-platform Windows Azure Command
Line Tools, available through npm.
</p>
        <h4>Installing the Azure CLI
</h4>
        <pre class="plain:nocontrols" name="code">npm install azure-cli –-global</pre>
        <p>
We can also use the Azure CLI to generate default Kudu scripts for our nodejs project.
Though we will need to make a few modifications to make the scripts work with Grunt,
it will give us a good start.
</p>
        <pre class="plain:nocontrols" name="code">azure site deploymentscript –-node</pre>
        <p>
This command will generate both our &lt;code&gt;.deployment&lt;/code&gt; and the default
&lt;code&gt;deploy.sh&lt;/code&gt;.
</p>
        <h4>Our .deployment file
</h4>
        <pre class="plain" name="code">[config]
command = bash ./deploy.sh</pre>
        <h3>Customizing deploy.sh for Grunt Deployment
</h3>
        <p>
From <code>.deployment</code>, Kudu will automatically execute our <code>deploy.sh</code> script.
Kudu’s default <code>deploy.sh</code> for a nodejs project will establish the environment
for node and npm as well as some supporting environment variables. It will also include
a "# Deployment" section containing all of the deployment steps. By default,
this will copy your repository contents to your /wwwroot, and then execute <code>npm
install --production</code> against wwwroot, as if installing the application's operating
dependencies. However, under Grunt, we want to execute tasks prior to <code>/wwwroot</code> deployment,
such as executing our Grunt tasks to compile LESS into CSS and CoffeeScript into JavaScript.
By replacing the entire Deployment section with the code below, we instruct Kudu to
perform the following tasks:
</p>
        <ol>
          <li>
Get the latest changes from Git (or Hg). This is done automatically before running
deploy.sh. 
</li>
          <li>
Run <code>npm install</code>, installing all dependencies, including those necessary
for development. 
</li>
          <li>
Optionally run <code>bower install</code>, if bower.json exists. This will update
our client-side JavaScript libraries. 
</li>
          <li>
Optionally run <code>grunt</code>, if Gruntfile.js exists. Below, I have grunt configured
to run the Clean, Common, and Dist tasks, which are LinemanJS's default tasks for
constructing a production-ready build. You can update the script to run whichever
tasks you need, or modify your Gruntfile to set these as the default tasks. 
</li>
          <li>
Finally, sync the contents of the prepared <code>/dist</code> directory to <code>/wwwroot</code>.
It is important to note that this is a KuduSync (similar to RSync), and not just a
copy. We only need to update the files that changed, which includes removing any obsolete
files. 
</li>
        </ol>
        <h4>Our deploy.sh file's Deployment Section
</h4>
        <pre class="plain:firstline[98]" name="code"># Deployment
# ----------

echo Handling node.js grunt deployment.

# 1. Select node version
selectNodeVersion

# 2. Install npm packages
if [ -e "$DEPLOYMENT_SOURCE/package.json" ]; then
  eval $NPM_CMD install
  exitWithMessageOnError "npm failed"
fi

# 3. Install bower packages
if [ -e "$DEPLOYMENT_SOURCE/bower.json" ]; then
  eval $NPM_CMD install bower
  exitWithMessageOnError "installing bower failed"
  ./node_modules/.bin/bower install
  exitWithMessageOnError "bower failed"
fi

# 4. Run grunt
if [ -e "$DEPLOYMENT_SOURCE/Gruntfile.js" ]; then
  eval $NPM_CMD install grunt-cli
  exitWithMessageOnError "installing grunt failed"
  ./node_modules/.bin/grunt --no-color clean common dist
  exitWithMessageOnError "grunt failed"
fi

# 5. KuduSync to Target
"$KUDU_SYNC_CMD" -v 500 -f "$DEPLOYMENT_SOURCE/dist" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;.deployment;deploy.sh"
exitWithMessageOnError "Kudu Sync to Target failed"</pre>
        <p>
These commands will execute bower and Grunt from local npm installations, rather than
the global space, as Windows Azure does not allow easy access to global installations.
Because bower and Grunt are manually installed based on the existence of bower.json
or Gruntfile.js, they also are not required to be referenced in your own package.json.
Finally, be sure to leave the –no-color flag enabled for Grunt execution, as the Azure
Deployment Logs will stumble when processing the ANSI color codes that are common
on Grunt output.
</p>
        <p>
Assuming that Git Deployment has already been configured, committing these files in
to Git will complete the process. Because the latest changes from Git are pulled before
executing the deployment steps, these two new files (<code>.deployment</code> and <code>deploy.sh</code>)
will be available when Kudu is ready for them.
</p>
        <h3>Troubleshooting
</h3>
        <h4>Long Directory Paths and the 260-Character Path Limit
</h4>
        <p>
Though Azure does a fantastic job of hosting nodejs projects, at the end of the day
Azure is still hosted on the Windows platform, and brings with it Windows limitations.
One of the issues that you will quickly run into under node is the <a href="http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath">260-Character
Path Limitation</a>. Under nodejs, the dependency tree for a node modules can get
rather deep. And because each dependency module loads up its own dependency modules
under its child folder structure, the folder structure can get rather deep, too. For
example, Lineman requires Testem, which requires Winston, which requires Request;
in the end, the directory tree can lead to ~/node_modules/lineman/node_modules/testem/node_modules/winston/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream,
which combined with the root path structure, can far exceed the 260 limit.
</p>
        <p>
          <i>The Workaround</i>
        </p>
        <p>
To reduce this nesting, make some of these dependencies into first-level dependencies.
With the nodejs dependency model, if a module has already been brought in at a higher
level, it is not repeated in the chain. Thus, if Request is made as a direct dependency
and listed in your project's project.json, it will no longer be nested under Winston,
splitting this single dependency branch in two:
</p>
        <ol>
          <li>
~/node_modules/lineman/node_modules/testem/node_modules/winston 
</li>
          <li>
~/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream 
</li>
        </ol>
        <p>
This is not ideal, but it will solve is a workaround for the Windows file structure
limitations. The element that you must be careful of is with dependency versioning,
as you will need to make sure your <code>package.json</code> references the appropriate
version of your pseudo-dependency; in this case, make sure your <code>package.json</code> references
the same version of Request as is referenced by Winston.
</p>
        <p>
To help find those deep dependencies, use <code>npm list</code>. It will show you
the full graph on the command line, supplying a handy visual indicator.
</p>
        <h4>__dirname vs Process.cwd()
</h4>
        <p>
In the node ecosystem, <code>Process.cwd()</code> is the current working directory
for the node process. There is also a common variable named <code>__dirname</code> that
is created by node; its value is the directory that contained your node script. If
you executed node against a script in the current working directory, then these values
should be the same. Except when they aren't, like in Windows Azure.
</p>
        <p>
In Windows Azure, everything is executed on the system drive, C:. Node and npm live
here, and it appears as though your deployment space does as well. However, this deployment
space is really a mapped directory, coming in from a network share where your files
are persisted. In Azure's node ecosystem, this means that your <code>Process.cwd()</code> is
the C-rooted path, while <code>__dirname</code> is the \\10.whatever-rooted UNC path
to your persisted files. Some Grunt-based tools and plugins (including Lineman) will
fail because that it will reference <code>__dirname</code> files while Grunt's core
is attempting to run tasks with the scope of <code>Process.cwd()</code>; Grunt recognizes
that it's trying to take action on \\10.whatever-rooted files in a C-rooted scope,
and fails because the files are not in a child directory.
</p>
        <p>
          <i>The Workaround</i>
        </p>
        <p>
If you are encountering this issue, reconfigure Grunt to work in the \\10.whatever-rooted
scope. You can do this by setting it's base path to <code>__dirname</code>, overriding
the default <code>Process.cwd()</code>. Within your Gruntfile.js, set the base path
immediately within your module export:
</p>
        <pre class="jscript" name="code">module.exports = function (grunt) {
  grunt.file.setBase(__dirname);
  // Code omitted
}</pre>
        <h4>Unable to find environment variable LINEMAN_MAIN
</h4>
        <p>
If like me, you are using Lineman to build your applications, you will encounter this
issue. Lineman manages Grunt and its configuration, so it prefers that all Grunt tasks
are executed via the Lineman CLI rather than directly executed via the Grunt CLI.
Lineman's Gruntfile.js includes a reference to an environment variable LINEMAN_MAIN,
set by the Lineman CLI, so that Grunt will run under the context of the proper Lineman
installation, which is what causes the failure if Grunt is executed directly.
</p>
        <p>
          <i>The Fix (Because this isn't a hack)</i>
        </p>
        <p>
Your development cycle has been configured to use lineman, so your deployment cycle
should use it, too! Update your <code>deploy.sh</code> Grunt execution to run Lineman
instead of Grunt. Also, since Lineman is referenced in your package.json, we don't
need to install it; it is already there.
</p>
        <p>
Option 1: deploy.sh
</p>
        <pre class="plain:firstline[120]" name="code"># 4. Run grunt
if [ -e "$DEPLOYMENT_SOURCE/Gruntfile.js" ]; then
  ./node_modules/.bin/lineman --no-color grunt clean common dist
  exitWithMessageOnError "lineman failed"
fi</pre>
        <p>
Recommendation: Since Lineman is wrapping Grunt for all of its tasks, consider simplifying <code>lineman
grunt clean common dist</code> into <code>lineman clean build</code>. You will still
need the <code>--no-color</code> flag, so that Grunt will not use ANSI color codes. 
</p>
        <p>
          <i>The Alternate Workaround</i>
        </p>
        <p>
If you don't want to change your <code>deploy.sh</code>—perhaps because you want to
maintain the generic file to handle all things Grunt—then as an alternative you can
update your <code>Gruntfile.js</code> to specify a default value for the missing <code>LINEMAN_MAIN</code> environment
variable. This environment variable is just a string value passed in to node's require
function, so that the right Lineman module can be loaded. Since Lineman is already
included in your <code>package.json</code>, it will already be available in the local <code>/node_modules</code> folder
because of the earlier <code>npm install</code> (deploy.sh, Step #2), and we can pass
'lineman' into <code>require( )</code> to have Grunt load the local Lineman installation.
Lineman will then supply its configuration into Grunt, and the system will proceed
as if you executed Lineman directly.
</p>
        <p>
Option 2: Gruntfile.js
</p>
        <pre class="jscript" name="code">module.exports = function(grunt) {
  grunt.file.setBase(__dirname);
  if (process.env['LINEMAN_MAIN'] === null || process.env['LINEMAN_MAIN'] === undefined) {
    process.env['LINEMAN_MAIN'] = 'lineman';
  }
  require(process.env['LINEMAN_MAIN']).config.grunt.run(grunt);
};</pre>
        <h3>Credits
</h3>
        <p>
Thank you to <a href="http://www.twitter.com/davidebbo">@davidebbo</a>, <a href="http://www.twitter.com/guayan">@guayan</a>, <a href="http://www.twitter.com/amitapl">@amitapl</a>,
and <a href="http://www.twitter.com/dburton">@dburton</a> for helping troubleshoot
Kudu and Grunt Deploy, making this all possible.
</p>
        <h3>Changelog
</h3>
        <p>
2013-12-03: Updated LINEMAN_MAIN Troubleshooting to improve resolution. Rather than
editing deploy.sh to set the environment variable, edit the file to execute Lineman.
This is the proper (and more elegant) solution. [Credit: <a href="http://www.twitter.com/searls">@searls</a>]
</p>
        <div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9b0ba700-7d7e-41fe-8c1b-8c35f6d0d010" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">Technorati
Tags: <a href="http://technorati.com/tags/Grunt" rel="tag">Grunt</a>,<a href="http://technorati.com/tags/Lineman" rel="tag">Lineman</a>,<a href="http://technorati.com/tags/Node" rel="tag">Node</a>,<a href="http://technorati.com/tags/Azure" rel="tag">Azure</a>,<a href="http://technorati.com/tags/Git" rel="tag">Git</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d" />
      </body>
      <title>Git and Grunt Deploy to Windows Azure</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d.aspx</guid>
      <link>http://www.cptloadtest.com/2013/12/03/Git-And-Grunt-Deploy-To-Windows-Azure.aspx</link>
      <pubDate>Tue, 03 Dec 2013 05:34:25 GMT</pubDate>
      <description>&lt;p&gt;
Azure Websites are a fantastic method of hosting your own web site. At Arana Software,
we use them often, particularly as test environments for our client projects. We can
quickly spin up a free site that is constantly up-to-date with the latest code using
continuous deployment from the project’s Git repository. Clients are able to see progress
on our development efforts without us having to worry about synchronizing codebases
or managing infrastructure. Since Windows Azure is a Microsoft offering, it is a natural
for handling .NET projects, but JavaScript-based nodejs is also a natural fit and
a first-class citizen on the Azure ecosystem.
&lt;/p&gt;
&lt;h3&gt;Incorporating Grunt
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://gruntjs.com/"&gt;Grunt&lt;/a&gt; is a JavaScript-based task runner for your
development projects to help you with those repetitive, menial, and mundane tasks
that are necessary for production readiness. This could be running unit tests, compiling
code, image processing, bundling and minification, and more. For our production deployments,
we commonly use Grunt to compile LESS into CSS, CoffeeScript into JavaScript, and
Jade into HTML, taking the code we write and preparing it for browser consumption.
We also use Grunt to optimize these various files for speed through bundling and minification.
The output of this work is done at deployment rather than development, with only the
source code committed into Git and never its optimized output.
&lt;/p&gt;
&lt;h3&gt;Git Deploy and Kudu
&lt;/h3&gt;
&lt;p&gt;
Continuous deployment will automatically update your site with the latest source code
whenever modifications are made to the source repository. This will also work with
Mercurial. There is plenty of existing documentation on &lt;a href="http://www.windowsazure.com/en-us/develop/nodejs/common-tasks/publishing-with-git/"&gt;setting
up Git Deploy in Azure&lt;/a&gt;, so consider that a prerequisite for this article. However,
Git Deploy, alone, will only take the files as they are in source, and directly deploy
them to the site. If you need to run additional tasks, such as compiling your .NET
source or running Grunt, that is where Kudu comes in.
&lt;/p&gt;
&lt;p&gt;
Kudu is the engine that drives Git deployments in Windows Azure. Untouched, it will
simply synchronize files from Git to your /wwwroot, but it can be easily reconfigured
to execute a deployment command, such as a Windows Command file, a Shell Script, or
a nodejs script. This is enabled through a standardized file named &amp;quot;.deployment&amp;quot;.
For Grunt deployment, we are going to execute a Shell Script that will perform npm,
Bower, and Grunt commands in an effort to make our code production-ready. For other
options on .deployment, check out the &lt;a href="https://github.com/projectkudu/kudu/wiki"&gt;Kudu
project wiki&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Kudu is also available locally for testing, and to help build out your deployment
scripts. The engine is available as a part of the cross-platform Windows Azure Command
Line Tools, available through npm.
&lt;/p&gt;
&lt;h4&gt;Installing the Azure CLI
&lt;/h4&gt;
&lt;pre class="plain:nocontrols" name="code"&gt;npm install azure-cli –-global&lt;/pre&gt;
&lt;p&gt;
We can also use the Azure CLI to generate default Kudu scripts for our nodejs project.
Though we will need to make a few modifications to make the scripts work with Grunt,
it will give us a good start.
&lt;/p&gt;
&lt;pre class="plain:nocontrols" name="code"&gt;azure site deploymentscript –-node&lt;/pre&gt;
&lt;p&gt;
This command will generate both our &amp;lt;code&amp;gt;.deployment&amp;lt;/code&amp;gt; and the default
&amp;lt;code&amp;gt;deploy.sh&amp;lt;/code&amp;gt;.
&lt;/p&gt;
&lt;h4&gt;Our .deployment file
&lt;/h4&gt;
&lt;pre class="plain" name="code"&gt;[config]
command = bash ./deploy.sh&lt;/pre&gt;
&lt;h3&gt;Customizing deploy.sh for Grunt Deployment
&lt;/h3&gt;
&lt;p&gt;
From &lt;code&gt;.deployment&lt;/code&gt;, Kudu will automatically execute our &lt;code&gt;deploy.sh&lt;/code&gt; script.
Kudu’s default &lt;code&gt;deploy.sh&lt;/code&gt; for a nodejs project will establish the environment
for node and npm as well as some supporting environment variables. It will also include
a &amp;quot;# Deployment&amp;quot; section containing all of the deployment steps. By default,
this will copy your repository contents to your /wwwroot, and then execute &lt;code&gt;npm
install --production&lt;/code&gt; against wwwroot, as if installing the application's operating
dependencies. However, under Grunt, we want to execute tasks prior to &lt;code&gt;/wwwroot&lt;/code&gt; deployment,
such as executing our Grunt tasks to compile LESS into CSS and CoffeeScript into JavaScript.
By replacing the entire Deployment section with the code below, we instruct Kudu to
perform the following tasks:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Get the latest changes from Git (or Hg). This is done automatically before running
deploy.sh. 
&lt;/li&gt;
&lt;li&gt;
Run &lt;code&gt;npm install&lt;/code&gt;, installing all dependencies, including those necessary
for development. 
&lt;/li&gt;
&lt;li&gt;
Optionally run &lt;code&gt;bower install&lt;/code&gt;, if bower.json exists. This will update
our client-side JavaScript libraries. 
&lt;/li&gt;
&lt;li&gt;
Optionally run &lt;code&gt;grunt&lt;/code&gt;, if Gruntfile.js exists. Below, I have grunt configured
to run the Clean, Common, and Dist tasks, which are LinemanJS's default tasks for
constructing a production-ready build. You can update the script to run whichever
tasks you need, or modify your Gruntfile to set these as the default tasks. 
&lt;/li&gt;
&lt;li&gt;
Finally, sync the contents of the prepared &lt;code&gt;/dist&lt;/code&gt; directory to &lt;code&gt;/wwwroot&lt;/code&gt;.
It is important to note that this is a KuduSync (similar to RSync), and not just a
copy. We only need to update the files that changed, which includes removing any obsolete
files. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Our deploy.sh file's Deployment Section
&lt;/h4&gt;
&lt;pre class="plain:firstline[98]" name="code"&gt;# Deployment
# ----------

echo Handling node.js grunt deployment.

# 1. Select node version
selectNodeVersion

# 2. Install npm packages
if [ -e &amp;quot;$DEPLOYMENT_SOURCE/package.json&amp;quot; ]; then
  eval $NPM_CMD install
  exitWithMessageOnError &amp;quot;npm failed&amp;quot;
fi

# 3. Install bower packages
if [ -e &amp;quot;$DEPLOYMENT_SOURCE/bower.json&amp;quot; ]; then
  eval $NPM_CMD install bower
  exitWithMessageOnError &amp;quot;installing bower failed&amp;quot;
  ./node_modules/.bin/bower install
  exitWithMessageOnError &amp;quot;bower failed&amp;quot;
fi

# 4. Run grunt
if [ -e &amp;quot;$DEPLOYMENT_SOURCE/Gruntfile.js&amp;quot; ]; then
  eval $NPM_CMD install grunt-cli
  exitWithMessageOnError &amp;quot;installing grunt failed&amp;quot;
  ./node_modules/.bin/grunt --no-color clean common dist
  exitWithMessageOnError &amp;quot;grunt failed&amp;quot;
fi

# 5. KuduSync to Target
&amp;quot;$KUDU_SYNC_CMD&amp;quot; -v 500 -f &amp;quot;$DEPLOYMENT_SOURCE/dist&amp;quot; -t &amp;quot;$DEPLOYMENT_TARGET&amp;quot; -n &amp;quot;$NEXT_MANIFEST_PATH&amp;quot; -p &amp;quot;$PREVIOUS_MANIFEST_PATH&amp;quot; -i &amp;quot;.git;.hg;.deployment;deploy.sh&amp;quot;
exitWithMessageOnError &amp;quot;Kudu Sync to Target failed&amp;quot;&lt;/pre&gt;
&lt;p&gt;
These commands will execute bower and Grunt from local npm installations, rather than
the global space, as Windows Azure does not allow easy access to global installations.
Because bower and Grunt are manually installed based on the existence of bower.json
or Gruntfile.js, they also are not required to be referenced in your own package.json.
Finally, be sure to leave the –no-color flag enabled for Grunt execution, as the Azure
Deployment Logs will stumble when processing the ANSI color codes that are common
on Grunt output.
&lt;/p&gt;
&lt;p&gt;
Assuming that Git Deployment has already been configured, committing these files in
to Git will complete the process. Because the latest changes from Git are pulled before
executing the deployment steps, these two new files (&lt;code&gt;.deployment&lt;/code&gt; and &lt;code&gt;deploy.sh&lt;/code&gt;)
will be available when Kudu is ready for them.
&lt;/p&gt;
&lt;h3&gt;Troubleshooting
&lt;/h3&gt;
&lt;h4&gt;Long Directory Paths and the 260-Character Path Limit
&lt;/h4&gt;
&lt;p&gt;
Though Azure does a fantastic job of hosting nodejs projects, at the end of the day
Azure is still hosted on the Windows platform, and brings with it Windows limitations.
One of the issues that you will quickly run into under node is the &lt;a href="http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath"&gt;260-Character
Path Limitation&lt;/a&gt;. Under nodejs, the dependency tree for a node modules can get
rather deep. And because each dependency module loads up its own dependency modules
under its child folder structure, the folder structure can get rather deep, too. For
example, Lineman requires Testem, which requires Winston, which requires Request;
in the end, the directory tree can lead to ~/node_modules/lineman/node_modules/testem/node_modules/winston/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream,
which combined with the root path structure, can far exceed the 260 limit.
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;The Workaround&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
To reduce this nesting, make some of these dependencies into first-level dependencies.
With the nodejs dependency model, if a module has already been brought in at a higher
level, it is not repeated in the chain. Thus, if Request is made as a direct dependency
and listed in your project's project.json, it will no longer be nested under Winston,
splitting this single dependency branch in two:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
~/node_modules/lineman/node_modules/testem/node_modules/winston 
&lt;/li&gt;
&lt;li&gt;
~/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
This is not ideal, but it will solve is a workaround for the Windows file structure
limitations. The element that you must be careful of is with dependency versioning,
as you will need to make sure your &lt;code&gt;package.json&lt;/code&gt; references the appropriate
version of your pseudo-dependency; in this case, make sure your &lt;code&gt;package.json&lt;/code&gt; references
the same version of Request as is referenced by Winston.
&lt;/p&gt;
&lt;p&gt;
To help find those deep dependencies, use &lt;code&gt;npm list&lt;/code&gt;. It will show you
the full graph on the command line, supplying a handy visual indicator.
&lt;/p&gt;
&lt;h4&gt;__dirname vs Process.cwd()
&lt;/h4&gt;
&lt;p&gt;
In the node ecosystem, &lt;code&gt;Process.cwd()&lt;/code&gt; is the current working directory
for the node process. There is also a common variable named &lt;code&gt;__dirname&lt;/code&gt; that
is created by node; its value is the directory that contained your node script. If
you executed node against a script in the current working directory, then these values
should be the same. Except when they aren't, like in Windows Azure.
&lt;/p&gt;
&lt;p&gt;
In Windows Azure, everything is executed on the system drive, C:. Node and npm live
here, and it appears as though your deployment space does as well. However, this deployment
space is really a mapped directory, coming in from a network share where your files
are persisted. In Azure's node ecosystem, this means that your &lt;code&gt;Process.cwd()&lt;/code&gt; is
the C-rooted path, while &lt;code&gt;__dirname&lt;/code&gt; is the \\10.whatever-rooted UNC path
to your persisted files. Some Grunt-based tools and plugins (including Lineman) will
fail because that it will reference &lt;code&gt;__dirname&lt;/code&gt; files while Grunt's core
is attempting to run tasks with the scope of &lt;code&gt;Process.cwd()&lt;/code&gt;; Grunt recognizes
that it's trying to take action on \\10.whatever-rooted files in a C-rooted scope,
and fails because the files are not in a child directory.
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;The Workaround&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
If you are encountering this issue, reconfigure Grunt to work in the \\10.whatever-rooted
scope. You can do this by setting it's base path to &lt;code&gt;__dirname&lt;/code&gt;, overriding
the default &lt;code&gt;Process.cwd()&lt;/code&gt;. Within your Gruntfile.js, set the base path
immediately within your module export:
&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;module.exports = function (grunt) {
  grunt.file.setBase(__dirname);
  // Code omitted
}&lt;/pre&gt;
&lt;h4&gt;Unable to find environment variable LINEMAN_MAIN
&lt;/h4&gt;
&lt;p&gt;
If like me, you are using Lineman to build your applications, you will encounter this
issue. Lineman manages Grunt and its configuration, so it prefers that all Grunt tasks
are executed via the Lineman CLI rather than directly executed via the Grunt CLI.
Lineman's Gruntfile.js includes a reference to an environment variable LINEMAN_MAIN,
set by the Lineman CLI, so that Grunt will run under the context of the proper Lineman
installation, which is what causes the failure if Grunt is executed directly.
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;The Fix (Because this isn't a hack)&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
Your development cycle has been configured to use lineman, so your deployment cycle
should use it, too! Update your &lt;code&gt;deploy.sh&lt;/code&gt; Grunt execution to run Lineman
instead of Grunt. Also, since Lineman is referenced in your package.json, we don't
need to install it; it is already there.
&lt;/p&gt;
&lt;p&gt;
Option 1: deploy.sh
&lt;/p&gt;
&lt;pre class="plain:firstline[120]" name="code"&gt;# 4. Run grunt
if [ -e &amp;quot;$DEPLOYMENT_SOURCE/Gruntfile.js&amp;quot; ]; then
  ./node_modules/.bin/lineman --no-color grunt clean common dist
  exitWithMessageOnError &amp;quot;lineman failed&amp;quot;
fi&lt;/pre&gt;
&lt;p&gt;
Recommendation: Since Lineman is wrapping Grunt for all of its tasks, consider simplifying &lt;code&gt;lineman
grunt clean common dist&lt;/code&gt; into &lt;code&gt;lineman clean build&lt;/code&gt;. You will still
need the &lt;code&gt;--no-color&lt;/code&gt; flag, so that Grunt will not use ANSI color codes. 
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;The Alternate Workaround&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
If you don't want to change your &lt;code&gt;deploy.sh&lt;/code&gt;—perhaps because you want to
maintain the generic file to handle all things Grunt—then as an alternative you can
update your &lt;code&gt;Gruntfile.js&lt;/code&gt; to specify a default value for the missing &lt;code&gt;LINEMAN_MAIN&lt;/code&gt; environment
variable. This environment variable is just a string value passed in to node's require
function, so that the right Lineman module can be loaded. Since Lineman is already
included in your &lt;code&gt;package.json&lt;/code&gt;, it will already be available in the local &lt;code&gt;/node_modules&lt;/code&gt; folder
because of the earlier &lt;code&gt;npm install&lt;/code&gt; (deploy.sh, Step #2), and we can pass
'lineman' into &lt;code&gt;require( )&lt;/code&gt; to have Grunt load the local Lineman installation.
Lineman will then supply its configuration into Grunt, and the system will proceed
as if you executed Lineman directly.
&lt;/p&gt;
&lt;p&gt;
Option 2: Gruntfile.js
&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;module.exports = function(grunt) {
  grunt.file.setBase(__dirname);
  if (process.env['LINEMAN_MAIN'] === null || process.env['LINEMAN_MAIN'] === undefined) {
    process.env['LINEMAN_MAIN'] = 'lineman';
  }
  require(process.env['LINEMAN_MAIN']).config.grunt.run(grunt);
};&lt;/pre&gt;
&lt;h3&gt;Credits
&lt;/h3&gt;
&lt;p&gt;
Thank you to &lt;a href="http://www.twitter.com/davidebbo"&gt;@davidebbo&lt;/a&gt;, &lt;a href="http://www.twitter.com/guayan"&gt;@guayan&lt;/a&gt;, &lt;a href="http://www.twitter.com/amitapl"&gt;@amitapl&lt;/a&gt;,
and &lt;a href="http://www.twitter.com/dburton"&gt;@dburton&lt;/a&gt; for helping troubleshoot
Kudu and Grunt Deploy, making this all possible.
&lt;/p&gt;
&lt;h3&gt;Changelog
&lt;/h3&gt;
&lt;p&gt;
2013-12-03: Updated LINEMAN_MAIN Troubleshooting to improve resolution. Rather than
editing deploy.sh to set the environment variable, edit the file to execute Lineman.
This is the proper (and more elegant) solution. [Credit: &lt;a href="http://www.twitter.com/searls"&gt;@searls&lt;/a&gt;]
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9b0ba700-7d7e-41fe-8c1b-8c35f6d0d010" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Grunt" rel="tag"&gt;Grunt&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Lineman" rel="tag"&gt;Lineman&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Node" rel="tag"&gt;Node&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Azure" rel="tag"&gt;Azure&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Git" rel="tag"&gt;Git&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,1a39fd38-1ee4-4c7d-85fd-4b10cb7ea54d.aspx</comments>
      <category>Azure</category>
      <category>Continuous Integration</category>
      <category>JavaScript</category>
      <category>Programming</category>
      <category>Task Automation</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=6eefe791-b04c-47f4-acf5-6aec10be5d0b</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,6eefe791-b04c-47f4-acf5-6aec10be5d0b.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,6eefe791-b04c-47f4-acf5-6aec10be5d0b.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6eefe791-b04c-47f4-acf5-6aec10be5d0b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In case you haven't heard, Ann Arbor GiveCamp 2011 is almost here. It is coming up
in two weeks, September 16-18. This will be the fourth annual Ann Arbor GiveCamp,
and it is again held at WCC. Sign up at <a href="http://www.annarborgivecamp.org">http://www.annarborgivecamp.org</a>.
</p>
        <p>
For anyone that is not aware of what GiveCamp is, it is a weekend long Coding-for-Charity
event where area software developers, database administrators, graphics designers,
and other technologists use their talents to meet the needs of area non-profits in
need of technology assistance. That need could range from a new intra-office application
to help manage donations and membership rosters, to a new web site spun up on a CMS
like Drupal, WordPress or DotNetNuke, or perhaps a new auto-notification system for
the blood bank to notify donors when they are again eligible to donate blood. The
event and its projects are completely platform agnostic and the sky is the limit;
there is only one rule: the project must be scoped for completion within one weekend.
</p>
        <p>
In today's economy, financial donations are down. People are unable to donate to the
area charities, or at least are unable to donate as much as they would like to or
once did. The local charities like the pet shelter are spending their dollars on dog
food and cat litter, and not on their web site and other marketing. As a result, their
web site was last refreshed in the 90s, or it was recently rebuilt by the proverbial
boss's son or neighbor-kid next door. Their online presence, if they have one at all,
doesn't meet the needs of the organization. It lacks a professional's touch.
</p>
        <p>
This year, 40 local charities are asking Ann Arbor GiveCamp to help them with their
technology needs. One of the charities for 2011 is Angels of Hope, a local organization
that helps local families whose children have pediatric cancer. Angels of Hope uses
nearly every dollar donated to pay for utility bills, home repairs, car payments,
and medical bills; unfortunately, sometimes that includes funeral services. Imagine
if you had a child with cancer; now imagine if you were to lose that child to the
disease and were unable to pay for the funeral. What Angels of Hope does to help out
the community is very inspiring and very humbling. But their web site is deficient;
made in 2001, it does not meet the needs of the organization, and does not do what
is required of a web site to market the organization and to provide necessary information
to those in need. They have asked for our help to provide a new site so that they
can boost their marketing effort, help bring in donations, and help get the word out
to other families that the support is there and available.
</p>
        <p>
There 39 other stories like Angel of Hope's. Forty organizations that give all of
themselves to improve lives of their neighborhoods, their communities, and people
in need. But in order to meet those needs, we need volunteers to complete projects.
So far, Ann Arbor GiveCamp only has enough volunteers to meet the needs of 7 charities.
Thirty-three charities are on a waiting list, crossing their fingers that more local
technologists will give up a weekend and donate their talents. Thirty-three stories
will go unheard without additional help from the very community that they seek to
improve.
</p>
        <p>
I encourage you to consider volunteering for Ann Arbor GiveCamp. We need your help,
the help of your developer friends, your graphic artist co-workers, and the database
administrators you have lunch with. Everyone--regardless of their talent level or
technology platform--is encouraged and requested to join us in two weeks, to be a
part of our inspiring and humbling event, and to join us as we help out those that
help others. When you cannot give with your wallets, this is a great opportunity to
give back to the community with your time and your talents. 
</p>
        <p>
While at Ann Arbor GiveCamp, we will do everything we can to help you complete projects.
Hosting accounts have been donated, domains names are pre-registered, and all the
food, snacks, and beverages you need to keep going will be on hand throughout the
weekend. We have the charities organized, the projects defined and scoped, and we
just need you to help get them done. 
</p>
        <p>
Thirty-three other charities are still waiting for word. Donate your weekend; sign
up to be a volunteer. Tell your friends and colleagues, and have them sign up to be
a volunteer. Forward this post on to other groups and mailing lists, and encourage
them to volunteer. Help GiveCamp meet its goal of forty completed projects on Sunday,
September 18th.
</p>
        <p>
Come be a part of something special.
</p>
        <p>
Learn more and sign up at <a href="http://www.annarborgivecamp.org">http://www.annarborgivecamp.org</a></p>
        <p>
We'll see you on the 16th.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ef5f7da8-a99c-4c7e-b3bc-a55e7b61fdd3" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Ann+Arbor" rel="tag">Ann Arbor</a>,<a href="http://technorati.com/tags/GiveCamp" rel="tag">GiveCamp</a>,<a href="http://technorati.com/tags/Geeks+Giving+Back" rel="tag">Geeks
Giving Back</a>,<a href="http://technorati.com/tags/Charity" rel="tag">Charity</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6eefe791-b04c-47f4-acf5-6aec10be5d0b" />
      </body>
      <title>Help Wanted: Ann Arbor GiveCamp 2011</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,6eefe791-b04c-47f4-acf5-6aec10be5d0b.aspx</guid>
      <link>http://www.cptloadtest.com/2011/09/06/Help-Wanted-Ann-Arbor-GiveCamp-2011.aspx</link>
      <pubDate>Tue, 06 Sep 2011 17:59:04 GMT</pubDate>
      <description>&lt;p&gt;
In case you haven't heard, Ann Arbor GiveCamp 2011 is almost here. It is coming up
in two weeks, September 16-18. This will be the fourth annual Ann Arbor GiveCamp,
and it is again held at WCC. Sign up at &lt;a href="http://www.annarborgivecamp.org"&gt;http://www.annarborgivecamp.org&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
For anyone that is not aware of what GiveCamp is, it is a weekend long Coding-for-Charity
event where area software developers, database administrators, graphics designers,
and other technologists use their talents to meet the needs of area non-profits in
need of technology assistance. That need could range from a new intra-office application
to help manage donations and membership rosters, to a new web site spun up on a CMS
like Drupal, WordPress or DotNetNuke, or perhaps a new auto-notification system for
the blood bank to notify donors when they are again eligible to donate blood. The
event and its projects are completely platform agnostic and the sky is the limit;
there is only one rule: the project must be scoped for completion within one weekend.
&lt;/p&gt;
&lt;p&gt;
In today's economy, financial donations are down. People are unable to donate to the
area charities, or at least are unable to donate as much as they would like to or
once did. The local charities like the pet shelter are spending their dollars on dog
food and cat litter, and not on their web site and other marketing. As a result, their
web site was last refreshed in the 90s, or it was recently rebuilt by the proverbial
boss's son or neighbor-kid next door. Their online presence, if they have one at all,
doesn't meet the needs of the organization. It lacks a professional's touch.
&lt;/p&gt;
&lt;p&gt;
This year, 40 local charities are asking Ann Arbor GiveCamp to help them with their
technology needs. One of the charities for 2011 is Angels of Hope, a local organization
that helps local families whose children have pediatric cancer. Angels of Hope uses
nearly every dollar donated to pay for utility bills, home repairs, car payments,
and medical bills; unfortunately, sometimes that includes funeral services. Imagine
if you had a child with cancer; now imagine if you were to lose that child to the
disease and were unable to pay for the funeral. What Angels of Hope does to help out
the community is very inspiring and very humbling. But their web site is deficient;
made in 2001, it does not meet the needs of the organization, and does not do what
is required of a web site to market the organization and to provide necessary information
to those in need. They have asked for our help to provide a new site so that they
can boost their marketing effort, help bring in donations, and help get the word out
to other families that the support is there and available.
&lt;/p&gt;
&lt;p&gt;
There 39 other stories like Angel of Hope's. Forty organizations that give all of
themselves to improve lives of their neighborhoods, their communities, and people
in need. But in order to meet those needs, we need volunteers to complete projects.
So far, Ann Arbor GiveCamp only has enough volunteers to meet the needs of 7 charities.
Thirty-three charities are on a waiting list, crossing their fingers that more local
technologists will give up a weekend and donate their talents. Thirty-three stories
will go unheard without additional help from the very community that they seek to
improve.
&lt;/p&gt;
&lt;p&gt;
I encourage you to consider volunteering for Ann Arbor GiveCamp. We need your help,
the help of your developer friends, your graphic artist co-workers, and the database
administrators you have lunch with. Everyone--regardless of their talent level or
technology platform--is encouraged and requested to join us in two weeks, to be a
part of our inspiring and humbling event, and to join us as we help out those that
help others. When you cannot give with your wallets, this is a great opportunity to
give back to the community with your time and your talents. 
&lt;/p&gt;
&lt;p&gt;
While at Ann Arbor GiveCamp, we will do everything we can to help you complete projects.
Hosting accounts have been donated, domains names are pre-registered, and all the
food, snacks, and beverages you need to keep going will be on hand throughout the
weekend. We have the charities organized, the projects defined and scoped, and we
just need you to help get them done. 
&lt;/p&gt;
&lt;p&gt;
Thirty-three other charities are still waiting for word. Donate your weekend; sign
up to be a volunteer. Tell your friends and colleagues, and have them sign up to be
a volunteer. Forward this post on to other groups and mailing lists, and encourage
them to volunteer. Help GiveCamp meet its goal of forty completed projects on Sunday,
September 18th.
&lt;/p&gt;
&lt;p&gt;
Come be a part of something special.
&lt;/p&gt;
&lt;p&gt;
Learn more and sign up at &lt;a href="http://www.annarborgivecamp.org"&gt;http://www.annarborgivecamp.org&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
We'll see you on the 16th.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ef5f7da8-a99c-4c7e-b3bc-a55e7b61fdd3" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Ann+Arbor" rel="tag"&gt;Ann Arbor&lt;/a&gt;,&lt;a href="http://technorati.com/tags/GiveCamp" rel="tag"&gt;GiveCamp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geeks+Giving+Back" rel="tag"&gt;Geeks
Giving Back&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Charity" rel="tag"&gt;Charity&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6eefe791-b04c-47f4-acf5-6aec10be5d0b" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,6eefe791-b04c-47f4-acf5-6aec10be5d0b.aspx</comments>
      <category>Events</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=0c56850a-f706-4eef-b9e2-1ac9b6153a5a</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,0c56850a-f706-4eef-b9e2-1ac9b6153a5a.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,0c56850a-f706-4eef-b9e2-1ac9b6153a5a.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=0c56850a-f706-4eef-b9e2-1ac9b6153a5a</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If there is one thing that I have learned throughout my career, it is that I need
to do what I do because I love it. I need to do what I do for me. There can be no
other reason: not because someone else wants me to do it, nor because of recognition
from another person or organization, nor for the money or the stature. Wherever direction
I take in my career, it must be because of my passion for the craft and my drive to
improve. Awards, money, and fame are all welcome side-effects that let me know that
others like what I do and think I do it well—this recognition is still rewarding,
and even more so, is an essential component to self-improvement—but that is all for
naught if I don't like what I am doing or I don't think I am doing it well. Awards,
money, and fame should purely serve as feedback, and not as motivation.
</p>
        <p>
I first got into computers because I wanted to see how they worked. My mother bought
the family's first computer while I was in high school, and I ran all of the little
programs that inexperienced users are never supposed to run. I ran them simply because
I wanted to see what they did, what purpose they served, and what would happen if
they were run by a novice user like I was at the time. I didn't stay a novice for
long, and I became quite adept at fixing broken systems, including everyone's favorite
command: format c:. I had to; it was the family computer, and if I broke it, I had
to fix it quickly or suffer the consequences.
</p>
        <p>
Soon, the door to programming was opened to me, and an entirely new frontier was available
for me to explore. With a shiny new copy of Visual Basic 3, I now had an opportunity
to write my own programs to learn and manipulate that computer to an even greater
degree. Now instead of black box programs doing bad things for unexplained reasons,
I had the opportunity to create my own evil-doings. Whether it was creating sprite-based
side-scrolling video games to blow up baddies or an investigation into "I wonder
what this module does," the opportunities—and the imagination—were endless. Memory
management and boot configurations for optimizing frame rates in Doom transformed
into a passion for performance optimization of enterprise-level applications. Finding
better ways to mail-bomb friends' AOL accounts without getting banned led to an obsession
for managing resources outside of immediate control. And those awful GeoCities and
Tripod sites filled with animated lava lamps and blinking text instilled both a drive
for a better user experience and a need to expertly manipulate the search engines
in my favor; I wanted users to find my little flag in the internet sand and to enjoy
their stay once they arrived.
</p>
        <p>
But somewhere along the path, I lost my way.
</p>
        <p>
I don't know how it happened, but it did. I lost my focus on pursuing the craft for
me, and was guided by external influences. I experienced burn out, an inability to
engage, and a complete lack of drive for what I had grown up doing. My passion was
gone. Blogging became more about keeping a schedule than it was about learning new
things. Community involvement became more about the pursuit of recognition than it
was about giving back to the community from which I had learned so much. Development
became a chore rather than a thrill. Work became…well…work.
</p>
        <p>
Last summer was my epiphany. I was one of the development leads for a client that
I was working with, and my fellow leads and I were interviewing candidates for a development
opening. Endless weeks of candidate after candidate left me feeling very uninspired.
One night I came home and was discussing with my wife that I just wished one candidate—just
one—was truly passionate about their craft; they would get my vote right away. I remember
saying "I can teach them to code, but I can't teach them to <em>want</em> to
code." And I remember that sinking feeling when I realized that I was describing
myself, too. I was the uninspired developer.
</p>
        <p>
I wouldn't hire me.
</p>
        <p>
Since that time, I've been slowly re-energizing. I eased off of community involvement,
my speaking engagements, my writing, and my pursuit of technology. I needed to assess
my entire plate, and identify what I was doing for Me and what I was doing for Them.
The items for Me were the only items that were kept. These Items For Me were the only
items that <em>could</em> be kept, else it was a futile exercise and I would never
reclaim my passion for my craft. It has taken a long time to process everything, to
figure out what I loved and what I didn't, what was important and what wasn't, and
above all how my passion stems from executing the plan with people and not for people.
</p>
        <p>
Executing the plan <em>with</em> people, and not <em>for</em> people.
</p>
        <p>
Early on in this process, I was working late at a client one night and a developer
that I highly respect spoke simply over the cube wall, "it's good to have you
back in the game, Jay." I had a long way to go on the new path, but at least
I knew it was the right one. I wish that I could tell you how I did it, how I rediscovered
my passion, but I think it would only dilute the message that it happened in the first
place. You have your own thing, your own love, your own approach, and your own nuances.
But you also already know each of those intimately, and you have either already found
that passion, or are ignoring it in favor of what's easy, what's comfortable, what
is expected of you, or worse, what has always been. 
</p>
        <p>
I challenge each of you to find your own passion. Pursue it. Realize it. Live it.
Thirteen colonies proclaimed to the world that everyone has the right to pursue happiness,
but do not confuse this with an entitlement to actually be happy; that part is entirely
on you. Your right—your responsibility—is to go after it. Or as the Great Morpheus
put it, "I can only show you the door. You're the one that has to walk through
it."
</p>
        <p>
Pursue your happiness. You already know what that thing is, and you already know how
to do it. The only person not allowing you to be happy is you. So stop working against
yourself, and walk through the door.
</p>
        <p>
Knock, knock, Neo.
</p>
        <p>
        </p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e22f7c96-991c-479e-be1f-7b6579526e09" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Craft" rel="tag">Craft</a>,<a href="http://technorati.com/tags/Soft+Skills" rel="tag">Soft
Skills</a>,<a href="http://technorati.com/tags/Passion" rel="tag">Passion</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=0c56850a-f706-4eef-b9e2-1ac9b6153a5a" />
      </body>
      <title>Find the Passion in Your Craft</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,0c56850a-f706-4eef-b9e2-1ac9b6153a5a.aspx</guid>
      <link>http://www.cptloadtest.com/2011/06/30/Find-The-Passion-In-Your-Craft.aspx</link>
      <pubDate>Thu, 30 Jun 2011 15:04:47 GMT</pubDate>
      <description>&lt;p&gt;
If there is one thing that I have learned throughout my career, it is that I need
to do what I do because I love it. I need to do what I do for me. There can be no
other reason: not because someone else wants me to do it, nor because of recognition
from another person or organization, nor for the money or the stature. Wherever direction
I take in my career, it must be because of my passion for the craft and my drive to
improve. Awards, money, and fame are all welcome side-effects that let me know that
others like what I do and think I do it well—this recognition is still rewarding,
and even more so, is an essential component to self-improvement—but that is all for
naught if I don't like what I am doing or I don't think I am doing it well. Awards,
money, and fame should purely serve as feedback, and not as motivation.
&lt;/p&gt;
&lt;p&gt;
I first got into computers because I wanted to see how they worked. My mother bought
the family's first computer while I was in high school, and I ran all of the little
programs that inexperienced users are never supposed to run. I ran them simply because
I wanted to see what they did, what purpose they served, and what would happen if
they were run by a novice user like I was at the time. I didn't stay a novice for
long, and I became quite adept at fixing broken systems, including everyone's favorite
command: format c:. I had to; it was the family computer, and if I broke it, I had
to fix it quickly or suffer the consequences.
&lt;/p&gt;
&lt;p&gt;
Soon, the door to programming was opened to me, and an entirely new frontier was available
for me to explore. With a shiny new copy of Visual Basic 3, I now had an opportunity
to write my own programs to learn and manipulate that computer to an even greater
degree. Now instead of black box programs doing bad things for unexplained reasons,
I had the opportunity to create my own evil-doings. Whether it was creating sprite-based
side-scrolling video games to blow up baddies or an investigation into &amp;quot;I wonder
what this module does,&amp;quot; the opportunities—and the imagination—were endless. Memory
management and boot configurations for optimizing frame rates in Doom transformed
into a passion for performance optimization of enterprise-level applications. Finding
better ways to mail-bomb friends' AOL accounts without getting banned led to an obsession
for managing resources outside of immediate control. And those awful GeoCities and
Tripod sites filled with animated lava lamps and blinking text instilled both a drive
for a better user experience and a need to expertly manipulate the search engines
in my favor; I wanted users to find my little flag in the internet sand and to enjoy
their stay once they arrived.
&lt;/p&gt;
&lt;p&gt;
But somewhere along the path, I lost my way.
&lt;/p&gt;
&lt;p&gt;
I don't know how it happened, but it did. I lost my focus on pursuing the craft for
me, and was guided by external influences. I experienced burn out, an inability to
engage, and a complete lack of drive for what I had grown up doing. My passion was
gone. Blogging became more about keeping a schedule than it was about learning new
things. Community involvement became more about the pursuit of recognition than it
was about giving back to the community from which I had learned so much. Development
became a chore rather than a thrill. Work became…well…work.
&lt;/p&gt;
&lt;p&gt;
Last summer was my epiphany. I was one of the development leads for a client that
I was working with, and my fellow leads and I were interviewing candidates for a development
opening. Endless weeks of candidate after candidate left me feeling very uninspired.
One night I came home and was discussing with my wife that I just wished one candidate—just
one—was truly passionate about their craft; they would get my vote right away. I remember
saying &amp;quot;I can teach them to code, but I can't teach them to &lt;em&gt;want&lt;/em&gt; to
code.&amp;quot; And I remember that sinking feeling when I realized that I was describing
myself, too. I was the uninspired developer.
&lt;/p&gt;
&lt;p&gt;
I wouldn't hire me.
&lt;/p&gt;
&lt;p&gt;
Since that time, I've been slowly re-energizing. I eased off of community involvement,
my speaking engagements, my writing, and my pursuit of technology. I needed to assess
my entire plate, and identify what I was doing for Me and what I was doing for Them.
The items for Me were the only items that were kept. These Items For Me were the only
items that &lt;em&gt;could&lt;/em&gt; be kept, else it was a futile exercise and I would never
reclaim my passion for my craft. It has taken a long time to process everything, to
figure out what I loved and what I didn't, what was important and what wasn't, and
above all how my passion stems from executing the plan with people and not for people.
&lt;/p&gt;
&lt;p&gt;
Executing the plan &lt;em&gt;with&lt;/em&gt; people, and not &lt;em&gt;for&lt;/em&gt; people.
&lt;/p&gt;
&lt;p&gt;
Early on in this process, I was working late at a client one night and a developer
that I highly respect spoke simply over the cube wall, &amp;quot;it's good to have you
back in the game, Jay.&amp;quot; I had a long way to go on the new path, but at least
I knew it was the right one. I wish that I could tell you how I did it, how I rediscovered
my passion, but I think it would only dilute the message that it happened in the first
place. You have your own thing, your own love, your own approach, and your own nuances.
But you also already know each of those intimately, and you have either already found
that passion, or are ignoring it in favor of what's easy, what's comfortable, what
is expected of you, or worse, what has always been. 
&lt;/p&gt;
&lt;p&gt;
I challenge each of you to find your own passion. Pursue it. Realize it. Live it.
Thirteen colonies proclaimed to the world that everyone has the right to pursue happiness,
but do not confuse this with an entitlement to actually be happy; that part is entirely
on you. Your right—your responsibility—is to go after it. Or as the Great Morpheus
put it, &amp;quot;I can only show you the door. You're the one that has to walk through
it.&amp;quot;
&lt;/p&gt;
&lt;p&gt;
Pursue your happiness. You already know what that thing is, and you already know how
to do it. The only person not allowing you to be happy is you. So stop working against
yourself, and walk through the door.
&lt;/p&gt;
&lt;p&gt;
Knock, knock, Neo.
&lt;/p&gt;
&lt;p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e22f7c96-991c-479e-be1f-7b6579526e09" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Craft" rel="tag"&gt;Craft&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Soft+Skills" rel="tag"&gt;Soft
Skills&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Passion" rel="tag"&gt;Passion&lt;/a&gt;
&lt;/div&gt;
&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=0c56850a-f706-4eef-b9e2-1ac9b6153a5a" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,0c56850a-f706-4eef-b9e2-1ac9b6153a5a.aspx</comments>
      <category>Business</category>
      <category>Mush</category>
      <category>Sharpening the Saw</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=245e0356-23d5-4212-99bf-583840b1e904</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=245e0356-23d5-4212-99bf-583840b1e904</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The first three parts of this series discuss the events that make up the ASP.NET Page
Life Cycle, the order that these events execute on the page and on the controls, and
that necessary evil called Data Binding, through which you add dynamic content to
your new ASP.NET page. Raising those events happens automatically, without any developer
oversight, but tapping in to those events—making them work for you—requires wiring
special methods called Event Handlers to those events. This wiring can happen automatically
by the system if you follow a few simple steps, or it can happen manually, with developers
connecting each appropriate event to its handler. Both sides have their advantages
as well as their drawbacks, but learning the nuances of event wiring can only strengthen
your ASP.NET repertoire.
</p>
        <blockquote>
          <h3>About the Series
</h3>
          <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
          <p>
Part 1: <a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx" target="_blank">Events
of the ASP.NET Page Life Cycle</a><br />
Part 2: <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">ASP.NET
Page &amp; WebControl Event Execution Order</a><br />
Part 3: <a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx">Getting
Started with ASP.NET DataBinding</a><br />
Part 4: Wiring Events to your Page
</p>
        </blockquote>
        <h3>Automatic Event Wireup
</h3>
        <p>
Page events can be automatically wired to their event handlers. That is, the code
to assign the event handler to your event can be made optional, with the system automatically
making the connection for the page, and thus, saving the developer a little bit of
work. Automatic event wiring is enabled through a boolean property called AutoEventWireup
on the ASP.NET Page Directive.
</p>
        <pre class="xml:nocontrols" name="code">&lt;%@ Page AutoEventWireup="true" %&gt;</pre>
        <p>
With this attribute set in the page directive, ASP.NET will automatically search for
and wire up any event handlers that match the specific naming convention of Page_<em>EventName</em>,
such as Page_Init or Page_Load. These methods can exist with or without the sender
and EventArgs method arguments, though do note they must both be absent or both be
present from the method signature; you cannot have one without the other.
</p>
        <pre class="csharp:nocontrols" name="code">protected void Page_Init()
{
  //Do some stuff
}

protected void Page_Load(object sender, EventArgs e)
{
  //Do some stuff
}</pre>
        <h3>Drawbacks to Event Auto-Wire
</h3>
        <p>
There is no fancy compiler magic to wire these event handlers to the event; under
AutoEventWireup, ASP.NET wires each event at run time when executing the page. This
means that for every event in the ASP.NET Page Life Cycle, the runtime engine reflects
through your code to search for an appropriate method for that event, just in case
it happens to exist, whether you create a handler method or not. To make matters worse,
it is reflecting for each event twice, once for a method with handler method arguments
(object sender, EventArgs e), and once for a method without the arguments.
</p>
        <p>
There are ten Life Cycle events on the Page that are eligible for auto-wire (counting
the Pre-and Post-Events such as PreInit and InitComplete) and four on the Control;
for a single page with five user controls, .NET Reflection is searching for up to
60 different methods. That is a lot of run-time overhead! Needless to say, AutoEventWireup
can have a significant negative impact on page performance.
</p>
        <h3>Manual Event Wireup
</h3>
        <p>
Page events can also be wired manually. This is similar to wiring up any other .NET
event. Using manual wire, you must explicitly wire the events you need to the appropriate
event handler. However, since you controls the wiring, this allows you to follow any
naming convention you wish and if needed, to wire multiple handlers to an event. Just
remember to turn AutoEventWireup off.
</p>
        <pre class="xml:nocontrols" name="code">&lt;%@ Page AutoEventWireup="false" %&gt;</pre>
        <p>
Page Events can be wired to their handler within your page's constructor.
</p>
        <pre class="csharp:nocontrols" name="code">public MyPageClass()
{
	Init += InitializePage;
	Load += LoadPage;
}

protected void InitializePage(object sender, EventArgs e)
{
  //Do some stuff
}

protected void LoadPage(object sender, EventArgs e)
{
  //Do some stuff
}</pre>
        <h3>Disadvantages to Manual Event Wireup
</h3>
        <p>
There is a little extra work involved to manually wire events. For the two events
from the auto-wire example, add a constructor and two lines of code. However, this
can be a bit of a daunting task if performed against an existing ASP.NET application
with dozens of pages and dozens more user controls. Even on new applications, it can
be easy to forget to wire the event, though it is easily identified in testing since
without the wireup, the handler does not execute, and in most cases, the page does
not function properly.
</p>
        <p>
Another disadvantage is that AutoEventWireup is enabled by default in C# (it is turned
off by default in Visual Basic). Even if you manually wire the events to handlers
in the page constructor, you must remember to disable wireup in the Page Directive.
Fail to do so will cause ASP.NET to wire up the events automatically in addition to
the manual hookup, and the event handler will be executed twice. So, again, don't
forget to turn off AutoEventWireup.
</p>
        <h3>Balancing Effort with Performance
</h3>
        <p>
Using AutoEventWireup essentially comes down to the value of developer effort versus
end-user performance. Auto-wire will save developers from one line of code per event,
but at the cost of system performance due to the reflection that occurs to have the
system wire the events rather than the developer. For a small utility web site or
a demo for a presentation, I can understand using auto-wire. However, being a performance
guy, I want to extract every ounce of horsepower I can out of my web applications,
so I will always opt for manual wiring; with it only costing me one line of code,
I feel that it is a good habit to have.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=245e0356-23d5-4212-99bf-583840b1e904" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</guid>
      <link>http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx</link>
      <pubDate>Tue, 26 Apr 2011 23:45:23 GMT</pubDate>
      <description>&lt;p&gt;
The first three parts of this series discuss the events that make up the ASP.NET Page
Life Cycle, the order that these events execute on the page and on the controls, and
that necessary evil called Data Binding, through which you add dynamic content to
your new ASP.NET page. Raising those events happens automatically, without any developer
oversight, but tapping in to those events—making them work for you—requires wiring
special methods called Event Handlers to those events. This wiring can happen automatically
by the system if you follow a few simple steps, or it can happen manually, with developers
connecting each appropriate event to its handler. Both sides have their advantages
as well as their drawbacks, but learning the nuances of event wiring can only strengthen
your ASP.NET repertoire.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;About the Series
&lt;/h3&gt;
&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;p&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx" target="_blank"&gt;Events
of the ASP.NET Page Life Cycle&lt;/a&gt; 
&lt;br /&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;ASP.NET
Page &amp;amp; WebControl Event Execution Order&lt;/a&gt; 
&lt;br /&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx"&gt;Getting
Started with ASP.NET DataBinding&lt;/a&gt; 
&lt;br /&gt;
Part 4: Wiring Events to your Page
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h3&gt;Automatic Event Wireup
&lt;/h3&gt;
&lt;p&gt;
Page events can be automatically wired to their event handlers. That is, the code
to assign the event handler to your event can be made optional, with the system automatically
making the connection for the page, and thus, saving the developer a little bit of
work. Automatic event wiring is enabled through a boolean property called AutoEventWireup
on the ASP.NET Page Directive.
&lt;/p&gt;
&lt;pre class="xml:nocontrols" name="code"&gt;&amp;lt;%@ Page AutoEventWireup=&amp;quot;true&amp;quot; %&amp;gt;&lt;/pre&gt;
&lt;p&gt;
With this attribute set in the page directive, ASP.NET will automatically search for
and wire up any event handlers that match the specific naming convention of Page_&lt;em&gt;EventName&lt;/em&gt;,
such as Page_Init or Page_Load. These methods can exist with or without the sender
and EventArgs method arguments, though do note they must both be absent or both be
present from the method signature; you cannot have one without the other.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;protected void Page_Init()
{
  //Do some stuff
}

protected void Page_Load(object sender, EventArgs e)
{
  //Do some stuff
}&lt;/pre&gt;
&lt;h3&gt;Drawbacks to Event Auto-Wire
&lt;/h3&gt;
&lt;p&gt;
There is no fancy compiler magic to wire these event handlers to the event; under
AutoEventWireup, ASP.NET wires each event at run time when executing the page. This
means that for every event in the ASP.NET Page Life Cycle, the runtime engine reflects
through your code to search for an appropriate method for that event, just in case
it happens to exist, whether you create a handler method or not. To make matters worse,
it is reflecting for each event twice, once for a method with handler method arguments
(object sender, EventArgs e), and once for a method without the arguments.
&lt;/p&gt;
&lt;p&gt;
There are ten Life Cycle events on the Page that are eligible for auto-wire (counting
the Pre-and Post-Events such as PreInit and InitComplete) and four on the Control;
for a single page with five user controls, .NET Reflection is searching for up to
60 different methods. That is a lot of run-time overhead! Needless to say, AutoEventWireup
can have a significant negative impact on page performance.
&lt;/p&gt;
&lt;h3&gt;Manual Event Wireup
&lt;/h3&gt;
&lt;p&gt;
Page events can also be wired manually. This is similar to wiring up any other .NET
event. Using manual wire, you must explicitly wire the events you need to the appropriate
event handler. However, since you controls the wiring, this allows you to follow any
naming convention you wish and if needed, to wire multiple handlers to an event. Just
remember to turn AutoEventWireup off.
&lt;/p&gt;
&lt;pre class="xml:nocontrols" name="code"&gt;&amp;lt;%@ Page AutoEventWireup=&amp;quot;false&amp;quot; %&amp;gt;&lt;/pre&gt;
&lt;p&gt;
Page Events can be wired to their handler within your page's constructor.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public MyPageClass()
{
	Init += InitializePage;
	Load += LoadPage;
}

protected void InitializePage(object sender, EventArgs e)
{
  //Do some stuff
}

protected void LoadPage(object sender, EventArgs e)
{
  //Do some stuff
}&lt;/pre&gt;
&lt;h3&gt;Disadvantages to Manual Event Wireup
&lt;/h3&gt;
&lt;p&gt;
There is a little extra work involved to manually wire events. For the two events
from the auto-wire example, add a constructor and two lines of code. However, this
can be a bit of a daunting task if performed against an existing ASP.NET application
with dozens of pages and dozens more user controls. Even on new applications, it can
be easy to forget to wire the event, though it is easily identified in testing since
without the wireup, the handler does not execute, and in most cases, the page does
not function properly.
&lt;/p&gt;
&lt;p&gt;
Another disadvantage is that AutoEventWireup is enabled by default in C# (it is turned
off by default in Visual Basic). Even if you manually wire the events to handlers
in the page constructor, you must remember to disable wireup in the Page Directive.
Fail to do so will cause ASP.NET to wire up the events automatically in addition to
the manual hookup, and the event handler will be executed twice. So, again, don't
forget to turn off AutoEventWireup.
&lt;/p&gt;
&lt;h3&gt;Balancing Effort with Performance
&lt;/h3&gt;
&lt;p&gt;
Using AutoEventWireup essentially comes down to the value of developer effort versus
end-user performance. Auto-wire will save developers from one line of code per event,
but at the cost of system performance due to the reflection that occurs to have the
system wire the events rather than the developer. For a small utility web site or
a demo for a presentation, I can understand using auto-wire. However, being a performance
guy, I want to extract every ounce of horsepower I can out of my web applications,
so I will always opt for manual wiring; with it only costing me one line of code,
I feel that it is a good habit to have.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=245e0356-23d5-4212-99bf-583840b1e904" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=15ae3174-d0bd-4639-9d9c-10d2feeaab35</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=15ae3174-d0bd-4639-9d9c-10d2feeaab35</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
"There was a time when everything was moving towards the desktop. This Internet
thing was new and cool, but there was no way it would ever last. And no one knew how
to code for the web, at least not anything beyond animated lava lamps and cute "Under
Construction" images. So, to make coding for the web easier, they made ASP.NET
to be just like coding for a desktop, using the same patterns, the same event-based
model, and the same stateful approach. But the web isn't stateful, its only events
are GET and POST, and is nothing like a desktop, so we tortured ourselves for years
forcing a square peg through a round hole. The time has come for redemption, and its
name is ASP.NET MVC. Spend an hour discovering how coding for the web is supposed
to be--how it is today--and end your misery. Salvation awaits."
</p>
        <div style="text-align: center">
          <div style="margin: 0px auto; width: 425px" id="__ss_4091487">
            <object id="__sse4091487" width="425" height="355">
              <param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" />
              <param name="allowFullScreen" value="true" />
              <param name="allowScriptAccess" value="always" />
              <embed name="__sse4091487" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355">
              </embed>
            </object>
          </div>
          <a title="Slides for &quot;ASP.NET MVC 2: A (Microsoft) Web Coder's Salvation&quot; on SlideShare" href="http://www.slideshare.net/jayharris/aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487">Slides
on SlideShare</a> | <a title="Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2" href="http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx">Code
Walkthrough</a></div>
        <p>
At various user groups throughout Southeast Michigan and Northwest Ohio, I have been
presenting this topic since the last quarter of 2009. For those that are interested,
I have made the slide deck available on SlideShare. The code demo used during the
talk is available as a walkthrough via one of my installments of Learn to Code, where
you can create, step-by-step, the same ASP.NET MVC 2 application as was built during
the presentation.
</p>
        <p>
If you attend one of my presentations for this topic, I would appreciate your feedback.
If you are willing to tolerate a small registration process, SpeakerRate will allow
you to give feedback and anonymous ratings to the session you attended. If you are
interested in having me present this topic at your user group or conference, please
contact me.
</p>
        <h3>Past presentations on this topic: 
</h3>
        <p>
          <a title="Rate my ASP.NET MVC 2 Talk at AADND, May 2010" href="http://speakerrate.com/talks/3235-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
Ann Arbor .NET Developers, Ann Arbor, Michigan, May 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk from GLUGnet Lansing, March 2010" href="http://speakerrate.com/talks/2865-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
GLUGnet, Lansing, Michigan, March 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at A2&lt;DIV&gt;, February 2010" href="http://speakerrate.com/talks/2175-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
A2&lt;DIV&gt;, Ann Arbor, Michigan, February 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at NWNUG, February 2010" href="http://speakerrate.com/talks/2134-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
NWNUG, Toledo, Ohio, February 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at GLUGnet Flint, February 2010" href="http://speakerrate.com/talks/3327-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
GLUGnet, Flint, Michigan, February 2010
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:97df05d2-ed4c-49b3-af36-91ee1db68b2a" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/Speaking" rel="tag">Speaking</a>,<a href="http://technorati.com/tags/Presentation" rel="tag">Presentation</a>,<a href="http://technorati.com/tags/Slides" rel="tag">Slides</a>,<a href="http://technorati.com/tags/Demo" rel="tag">Demo</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=15ae3174-d0bd-4639-9d9c-10d2feeaab35" />
      </body>
      <title>Presenting ASP.NET MVC 2: A (Microsoft) Web Coder's Salvation</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</guid>
      <link>http://www.cptloadtest.com/2010/05/23/Presenting-ASPNET-MVC-2-A-Microsoft-Web-Coders-Salvation.aspx</link>
      <pubDate>Sun, 23 May 2010 22:40:39 GMT</pubDate>
      <description>&lt;p&gt;
&amp;quot;There was a time when everything was moving towards the desktop. This Internet
thing was new and cool, but there was no way it would ever last. And no one knew how
to code for the web, at least not anything beyond animated lava lamps and cute &amp;quot;Under
Construction&amp;quot; images. So, to make coding for the web easier, they made ASP.NET
to be just like coding for a desktop, using the same patterns, the same event-based
model, and the same stateful approach. But the web isn't stateful, its only events
are GET and POST, and is nothing like a desktop, so we tortured ourselves for years
forcing a square peg through a round hole. The time has come for redemption, and its
name is ASP.NET MVC. Spend an hour discovering how coding for the web is supposed
to be--how it is today--and end your misery. Salvation awaits.&amp;quot;
&lt;/p&gt;
&lt;div style="text-align: center"&gt;
&lt;div style="margin: 0px auto; width: 425px" id="__ss_4091487"&gt;
&lt;object id="__sse4091487" width="425" height="355"&gt;
&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowScriptAccess" value="always" /&gt;&lt;embed name="__sse4091487" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"&gt;&lt;/embed&gt;
&lt;/object&gt;
&lt;/div&gt;
&lt;a title="Slides for &amp;quot;ASP.NET MVC 2: A (Microsoft) Web Coder&amp;#39;s Salvation&amp;quot; on SlideShare" href="http://www.slideshare.net/jayharris/aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487"&gt;Slides
on SlideShare&lt;/a&gt; | &lt;a title="Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2" href="http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx"&gt;Code
Walkthrough&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;
At various user groups throughout Southeast Michigan and Northwest Ohio, I have been
presenting this topic since the last quarter of 2009. For those that are interested,
I have made the slide deck available on SlideShare. The code demo used during the
talk is available as a walkthrough via one of my installments of Learn to Code, where
you can create, step-by-step, the same ASP.NET MVC 2 application as was built during
the presentation.
&lt;/p&gt;
&lt;p&gt;
If you attend one of my presentations for this topic, I would appreciate your feedback.
If you are willing to tolerate a small registration process, SpeakerRate will allow
you to give feedback and anonymous ratings to the session you attended. If you are
interested in having me present this topic at your user group or conference, please
contact me.
&lt;/p&gt;
&lt;h3&gt;Past presentations on this topic: 
&lt;/h3&gt;
&lt;p&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at AADND, May 2010" href="http://speakerrate.com/talks/3235-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
Ann Arbor .NET Developers, Ann Arbor, Michigan, May 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk from GLUGnet Lansing, March 2010" href="http://speakerrate.com/talks/2865-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
GLUGnet, Lansing, Michigan, March 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at A2&amp;lt;DIV&amp;gt;, February 2010" href="http://speakerrate.com/talks/2175-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
A2&amp;lt;DIV&amp;gt;, Ann Arbor, Michigan, February 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at NWNUG, February 2010" href="http://speakerrate.com/talks/2134-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
NWNUG, Toledo, Ohio, February 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at GLUGnet Flint, February 2010" href="http://speakerrate.com/talks/3327-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
GLUGnet, Flint, Michigan, February 2010
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:97df05d2-ed4c-49b3-af36-91ee1db68b2a" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Speaking" rel="tag"&gt;Speaking&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Presentation" rel="tag"&gt;Presentation&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Slides" rel="tag"&gt;Slides&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Demo" rel="tag"&gt;Demo&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=15ae3174-d0bd-4639-9d9c-10d2feeaab35" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>MVC</category>
      <category>Speaking</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=5a613524-5607-4fbb-a89f-74b5afb7c5e9</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,5a613524-5607-4fbb-a89f-74b5afb7c5e9.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,5a613524-5607-4fbb-a89f-74b5afb7c5e9.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5a613524-5607-4fbb-a89f-74b5afb7c5e9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Aligned with another jam session at Ann Arbor's <a href="http://www.comejamwithus.org">Come
Jam With Us</a> is another installment of Learn to Code, this time providing an introduction
to WatiN, or Web Application Testing in .NET. The jam session was held at the offices
of SRT Solutions in Ann Arbor, Michigan, at 5:30p, Tuesday April 6th. Though thunderstorms
were in the forecast, the predicted high was 72°F (22°C), so we weren't bothered by
the same 8" of fluffy white snow that caused cancellations and delays during
the my session on ASP.NET MVC 2. But for those that couldn't make the WatiN jam session,
might I recommend the exercise below.
</p>
        <h3>About This Exercise
</h3>
        <p>
This coding exercise is designed to give you an introduction to browser-based testing
using the WatiN framework, or Web Application Testing in .NET. The framework allows
developers to create integration tests (using a unit testing framework like MbUnit,
NUnit, or MSTest) to test and assert their application within a browser window. The
framework interacts with the browser DOM much like and end-user, producing reliable
results that mimic the real world. In this sample, we will write a few WatiN tests
against the Google search engine.
</p>
        <h3>Prerequisites
</h3>
        <p>
To complete this exercise, you will need to meet or complete a few prerequisites.
Please complete these prerequisites before moving on. The session is designed to be
completed in about an hour, but setup and prerequisites are not included in that time.
</p>
        <ul>
          <li>
An active internet connection. (Our tests will be conducted against live third-party
sites.) 
</li>
          <li>
Install Microsoft Visual Studio 2008 or Microsoft Visual Studio 2010. 
</li>
          <li>
            <a title="Download WATiN from SourceForge" href="http://sourceforge.net/projects/watin/files/">Download</a> and
extract the latest version of the WatiN framework. 
</li>
        </ul>
        <h3>Exercise 0: Getting Started
</h3>
        <h4>Creating a Project
</h4>
        <p>
WatiN is generally used within the context of a unit testing framework. For this exercise,
we will be using a Visual Studio Test Project and MSTest to wrap our WatiN code.
</p>
        <ol>
          <li>
Create a new "Test Project" in Visual Studio named "WatinSample".
The language is up to you, but all of the examples in this post will use C#. 
</li>
          <li>
Feel free to delete the Authoring Tests document, the Manual Test file, and UnitTest1.cs.
We won't be using these. 
</li>
          <li>
Add a reference to WatiN.Core.dll from the bin directory of your extracted WatiN download. 
</li>
          <li>
Compile. 
</li>
        </ol>
        <h3>Exercise 1: My First Browser Tests
</h3>
        <p>
In our first test, we will use the project we just created to test Google's home page.
After accessing <a href="http://www.google.com">http://www.google.com</a>, we will
check a few properties of the browser and a few loaded elements to ensure that the
expected page was returned. The first thing we will need is a new Unit Test class
to start our testing.
</p>
        <ol>
          <li>
Create a new class (Right click on the "WatinSample" project and select
Add –&gt; Class…), called WhenViewingTheGoogleHomePage. 
</li>
          <li>
Mark the class as public. 
</li>
          <li>
Add the MSTest [TestClass] attribute to the new class. 
</li>
          <li>
Compile. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingTheGoogleHomePage
  {
  }
}</pre>
        <h4>Make an instance of the browser
</h4>
        <p>
Now that we have a test class, we can start writing WatiN code. Each of our tests
will first need a Browser object to test against. Using methods attributed with TestInitialize
and TestCleanup, we can create a browser instance before the test starts and shut
it down when the test is complete.
</p>
        <p>
Creating an instance of a browser in WatiN is easy: simply create a new instance of
the IE class, passing in a URL. We can assign this new class to a field of type Browser,
which is a base class of all browser classes in WatiN. Currently, WatiN supports Internet
Explorer and Firefox.
</p>
        <ol>
          <li>
Create a private field in the test class named browserInstance of type WatiN.Core.Browser.
Add a using statement to WatiN.Core if you wish. 
</li>
          <li>
Create a test initialization method named WithAnInstanceOfTheBrowser and give it the
[TestInitialize] attribute. Within this method, create a new instance of the IE class,
passing in the Google URL, <a href="http://www.google.com">http://www.google.com</a>,
and assigning the instance to the browserInstance field. 
</li>
          <li>
Finally, create a test cleanup method named ShutdownBrowserWhenDone and give it the
[TestCleanup] attribute. Within this method, execute the Close() method on our browser
instance and assign the field to null to assist with object disposal. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingTheGoogleHomePage
  {
    Browser browserInstance;

    [TestInitialize]
    public void WithAnInstanceOfTheBrowser()
    {
      browserInstance = new IE("http://www.google.com");
    }

    [TestCleanup]
    public void ShutdownBrowserWhenDone()
    {
      browserInstance.Close();
      browserInstance = null;
    }
  }
}</pre>
        <h4>Our First Tests: Checking for existence of an element
</h4>
        <p>
There are three prominent items on the Google home page: the Google logo, the search
criteria text box, and the search button. Using WatiN, we can check for them all.
The WatiN Browser object contains an Elements collection, which is a flattened collection
of every element in the entire DOM. Like any collection, you can use Linq and lambda
expressions to search for items within this collection. Alternately, you may also
use the Element method, which accepts the same lambda expression that would be used
within the Where extension method on the collection, and returns the first or default
element. For more specific searches, WatiN's Browser object includes similar collections
and methods for searching explicitly for Images (&lt;IMG&gt;), Paras (&lt;P&gt;),
Text Fields (&lt;INPUT type="text" /&gt;), and so on.
</p>
        <p>
On each returned Element (or derived Para, Image, or Text Field, etc., all of which
inherit from Element), WatiN supplies properties for accessing the CSS Class, Id,
InnerHtml, Name, Tag, Text, Value, or many other attributes. The method GetAttributeValue(string
attributeName) is provided for accessing other attributes that are not explicitly
defined on the object (uncommon attributes and custom attributes). Finally, elements
also contain a Style property, which not only gives access to the inline style attribute,
but also any CSS properties associated with the element from Internal Style (in the
Page Head) or External Style (in an external style sheet).
</p>
        <p>
On to checking for the three elements within the Google home page: the logo, the criteria
input, and the search button. First, check for the existence of the Google logo graphic.
The image can be found by searching the DOM for an image with an Id of "logo".
WatiN works very closely with lambda expressions, so we can use these to help us find
out graphic.
</p>
        <ol>
          <li>
Create a new public method named PageShouldContainGoogleLogo. 
</li>
          <li>
Add the MSTest [TestMethod] attribute to the method. 
</li>
          <li>
Search for and assert on the existence of an image with the Id of "logo". 
</li>
          <li>
Optionally, we can also check that the image has the expected Alt attribute; in this
case, the value should be "Google". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void PageShouldContainGoogleLogo()
{
  Image googleLogo;
  googleLogo = browserInstance.Image(img =&gt; img.Id == "logo");
  Assert.IsTrue(googleLogo.Exists);
  Assert.AreEqual("Google", googleLogo.Alt);
}</pre>
        <p>
Next, check for the existence of the search criteria input box. WatiN refers to these
elements as Text Fields, using the TextField type. Additionally, this form field is
identified by its Name rather than its Id. In Google, the name given to the criteria
input is "q".
</p>
        <ol>
          <li>
Create a new public method named PageShouldContainSearchCriteriaInput and give it
the [TestMethod] attribute. 
</li>
          <li>
Search for and assert on the existence of a Text Field with the name "q". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void PageShouldContainSearchCriteriaInput()
{
  TextField criteriaInput;
  criteriaInput = browserInstance.TextField(tf =&gt; tf.Name == "q");
  Assert.IsTrue(criteriaInput.Exists);
}</pre>
        <p>
Finally, check for the existence of the search button using the Button method. In
our lambda expression, it is not important to know if the field is identified by a
Name property or an Id attribute, as WatiN supplies a IdOrName property to help us
find the element. The value to identify the button is "btnG".
</p>
        <ol>
          <li>
Create a new public method named PageShouldContainSearchButton and give it the [TestMethod]
attribute. 
</li>
          <li>
Search for and assert on the existence of a Button with the Id or Name of 'btnG". 
</li>
          <li>
Optionally, we can also check the value of the button, which is the text displayed
on the button on-screen. This text should be "Google Search". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void PageShouldContainSearchButton()
{
  Button searchButton;
  searchButton = browserInstance.Button(btn =&gt; btn.IdOrName == "btnG");
  Assert.IsTrue(searchButton.Exists);
  Assert.AreEqual("Google Search", searchButton.Value);
}</pre>
        <h4>Working with Style
</h4>
        <p>
WatiN can access properties on the DOM beyond just Text values and Alt attributes.
WatiN also has full access to the style that CSS has applied to an element. Let's
check out a few CSS properties, both those explicitly defined by WatiN and those implicitly
accessible through the WatiN framework.
</p>
        <p>
For our first style check, we'll take a look at the default font family used on the
Google Home Page. Font Family is one of the explicitly available style properties
on a WatiN element. Some others, like Color, Display, and Height are also explicitly
defined.
</p>
        <ol>
          <li>
Create a new public test method named BodyShouldUseArialFontFamily. 
</li>
          <li>
Assert that the font family assigned to the body matches "arial, sans-serif". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void BodyShouldUseArialFontFamily()
{
  Assert.AreEqual("arial, sans-serif", browserInstance.Body.Style.FontFamily);
}</pre>
        <p>
For our second style check, we will look for an implicit style definition. At the
top of the Google Home Page is a series of links to other areas of Google, such as
Images, Videos, Maps, and News. At the end of this list is a More link, that when
clicked, displays a hidden DIV tag containing even more links, such as Books, Finance,
and Google Translate. Since we do not have any code in our test initialization that
interacts with the browser, and thus nothing that is clicking the More link, that
DIV should still have a hidden visibility. However, since Visibility isn't an explicitly
defined style property within WatiN, we need to use the GetAttributeValue method to
retrieve the current visibility setting.
</p>
        <ol>
          <li>
Create a new public test method named MoreItemsShouldNotBeVisibleOnPageLoad. 
</li>
          <li>
Search for the More Items DIV. It's Id is "gbi". 
</li>
          <li>
Using the property lookup method, GetAttributeValue(string attributeName), check that
the Visibility is set to "hidden". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void MoreItemsShouldNotBeVisibleOnPageLoad()
{
  var googleBarMoreItems = browserInstance.Div(gbi =&gt; gbi.Id == "gbi");
  Assert.AreEqual("hidden", googleBarMoreItems.Style.GetAttributeValue("visibility"));
}</pre>
        <h3>Exercise 2: Interacting with the Browser
</h3>
        <p>
Browser Integration tests are more than just loading a page and checking a few element
attributes. Our tests may also need to enter values into form fields, click links
and buttons, or interact with browser navigation like the back button. WatiN fully
supports all of these features in a very intuitive fashion.
</p>
        <h4>A new test class, this time with Search Capability
</h4>
        <p>
Create a new test class, similar to what we did in Exercise 1, calling the new test
class WhenViewingGoogleSearchResultsForComeJamWithUs. Also add in the TestInitialize
and TestCleanup methods that open and close the browser. However, this time, after
we load <a href="http://www.google.com">http://www.google.com</a>, enter a value into
the search criteria input and then click the Google Search button.
</p>
        <ol>
          <li>
Create a new class named WhenViewingGoogleSearchResultsForComeJamWithUs, similar to
what was done in Exercise 1. 
</li>
          <li>
Add in the TestInitialize and TestCleanup methods from Exercise 1. Name the Initialize
method WithAnInstanceOfTheBrowserSearchingGoogle. 
</li>
          <li>
After the code that initializes the IE class, find the search criteria Text Field
and set its value to "Come Jam With Us". 
</li>
          <li>
After setting the Text Field value, click the Google Search button by calling the
Click() method on the Button class. 
</li>
          <li>
Compile. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingGoogleSearchResultsForComeJamWithUs
  {
    Browser browserInstance;

    [TestInitialize]
    public void WithAnInstanceOfTheBrowserSearchingGoogle()
    {
      browserInstance = new IE(@"http://www.google.com");
      TextField criteria =
        browserInstance.TextField(tf =&gt; tf.Name == "q");
      criteria.Value = "Come Jam With Us";
      Button search =
        browserInstance.Button(btn =&gt; btn.IdOrName == "btnG");
      search.Click();
    }

    [TestCleanup]
    public void ShutdownBrowserWhenDone()
    {
      browserInstance.Close();
      browserInstance = null;
    }
  }
}</pre>
        <p>
With this code, or initialized test will load the Google Home Page and will conduct
a search for "Come Jam With Us".
</p>
        <h4>Validating the Search Results Page
</h4>
        <p>
For our first verification, let's check the URL for the browser window. The search
result URL should contain the search criteria in the URL's query string; we can validate
this using the URL property on our instance of the Browser object.
</p>
        <ol>
          <li>
Create a new public test method named BrowserUrlShouldContainSearchCriteria. 
</li>
          <li>
Validate that the current browser URL contains the search criteria information, "q=Come+Jam+With+Us". 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void BrowserUrlShouldContainSearchCriteria()
{
  Assert.IsTrue(browserInstance.Url.Contains(@"q=Come+Jam+With+Us"));
}</pre>
        <h4>Finding Child Elements
</h4>
        <p>
With WatiN, we are not just limited to searching for items directly from the Browser
object. We can also search for child elements directly from their parent element or
any ancestor element. Our search results should contain a search result item linking
to the Come Jam With Us web site. The Google Results page contains a DIV identified
as "res" that serves as a container for all search result information. Rather
than checking that our Come Jam With Us link exists somewhere on the page, we should
search for it directly within the results DIV.
</p>
        <ol>
          <li>
Create a new public test method named ResultsShouldContainLinkToComeJamWithUs. 
</li>
          <li>
From the browser instance, find a DIV identified as "res". 
</li>
          <li>
Assert that a link to <a href="http://www.comejamwithus.org">http://www.comejamwithus.org</a> exists
within the "res" DIV. 
</li>
          <li>
Compile and run the test. The test should pass. 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void ResultsShouldContainLinkToComeJamWithUs()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&gt; div.IdOrName == "res");
  comeJamWithUs =
    searchResults.Link(link =&gt; link.Url == @"http://www.comejamwithus.org/");
  Assert.IsTrue(comeJamWithUs.Exists);
}</pre>
        <h4>Inner Text verses InnerHtml
</h4>
        <p>
An element may contain many child elements. An anchor tag—&lt;A href="#"&gt;—can
contain text, and child elements may make portions of that text bold, italic, underlined,
or even bright red. Through WatiN, we can access that inner content as straight text
without the formatting, or as the InnerHtml including all of the child elements.
</p>
        <ol>
          <li>
Create two public test methods, one named ResultsLinkContainsComeJamWithUsText and
the other named ResultsLinkContainsComeJamWithUsHtml. 
</li>
          <li>
In both methods, search for the results DIV, as we did in the previous test. 
</li>
          <li>
In both methods, search through the results DIV for a link with a URL matching <a href="http://www.comejamwithus.org">http://www.comejamwithus.org</a></li>
          <li>
In the Text method, assert that the text of the link matches "Come Jam with us
(Software Development Study Group)". Note that the value contains no child HTML
elements. 
</li>
          <li>
In the HTML method, assert that the InnerHtml of the link matches "&lt;EM&gt;Come
Jam with us&lt;/EM&gt; (Software Development Study Group)". Note that for the
same link, we now have the emphasis tags surrounding Come Jam With Us. 
</li>
          <li>
Compile and run both tests. The tests should pass.</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void ResultsLinkContainsComeJamWithUsText()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&gt; div.IdOrName == "res");
  comeJamWithUs =
    searchResults.Link(link =&gt; link.Url == @"http://www.comejamwithus.org/");
  Assert.AreEqual(@"Come Jam with us (Software Development Study Group)",
    comeJamWithUs.Text);
}

[TestMethod]
public void ResultsLinkContainsComeJamWithUsHtml()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&gt; div.IdOrName == "res");
  comeJamWithUs =
    searchResults.Link(link =&gt; link.Url == @"http://www.comejamwithus.org/");
  Assert.AreEqual(
    @"&lt;EM&gt;Come Jam with us&lt;/EM&gt; (Software Development Study Group)",
    comeJamWithUs.InnerHtml);
}</pre>
        <h4>Back to the Start
</h4>
        <p>
As previously mentioned, we can also fully interact with the browser, itself. Our
test initialization started from the Google Home Page and performed a search. Using
functionality built in to WatiN, we can execute the browser's back navigation to return
to the previous page.
</p>
        <p>
For our next test, execute a back navigation and verify that the browser's URL matches <a href="http://www.google.com/">http://www.google.com/</a>.
</p>
        <ol>
          <li>
Create a public test method named PageShouldHaveComeFromGoogleDotCom. 
</li>
          <li>
Execute back navigation in the browser by calling the Back() method on browserInstance. 
</li>
          <li>
Validate that the browser URL matches <a href="http://www.google.com/">http://www.google.com/</a>. 
</li>
          <li>
Compile and run the test. The test should pass.</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void PageShouldHaveComeFromGoogleDotCom()
{
  string previousUrl;
  browserInstance.Back();
  previousUrl = browserInstance.Url;
  Assert.AreEqual(@"http://www.google.com/", previousUrl);
}</pre>
        <h4>Putting it all together
</h4>
        <p>
Some interactions on a page cause element properties to change. An example of this
is the More link from Exercise 1; when the end-user clicks the More link, the More
Items DIV appears because the link's click event changes the Visibility style property
of the DIV to visible. For our final test, we will use what we have learned to test
this functionality.
</p>
        <ol>
          <li>
Create a new public test method named MoreItemsShouldBeVisibleOnMoreLinkClick.</li>
          <li>
Search for the header bar of Google links, a DIV with an Id of "gbar".</li>
          <li>
Within "gbar", search for the More Items DIV by an Id or Name of "gbi". 
</li>
          <li>
Assert that the visibility style property has a value of "hidden". 
</li>
          <li>
Within "gbar", search for the More link by its class name, "gb3".
Note that since a class attribute may contain multiple class definitions, this is
accomplished by validating that the class attribute contains the class you are searching
for. 
</li>
          <li>
Execute a Click event on the link. 
</li>
          <li>
Assert that the visibility style property of the More Items DIV has changed to "visible". 
</li>
        </ol>
        <pre class="csharp:nocontrols" name="code">[TestMethod]
public void MoreItemsShouldBeVisibleOnMoreLinkClick()
{
  var googleBar = browserInstance.Div(gbar =&gt; gbar.Id == "gbar");
  var googleBarMoreItems = googleBar.Div(gbi =&gt; gbi.Id == "gbi");
  Assert.AreEqual("hidden",
    googleBarMoreItems.Style.GetAttributeValue("visibility"));
  var googleBarMoreLink =
    googleBar.Link(link =&gt; link.ClassName.Contains("gb3"));
  googleBarMoreLink.Click();
  Assert.AreEqual("visible",
    googleBarMoreItems.Style.GetAttributeValue("visibility"));
}</pre>
        <h4>That's It
</h4>
        <p>
Now that we have spent some time on basic properties, interactions, and style sheets
within the WatiN framework, hopefully you can apply this to your own application and
get started with your own browser-based integration tests. If you would like more
information, I encourage you to check out the WatiN site at <a href="http://watin.sourceforge.net">http://watin.sourceforge.net</a>.
And as always, if you have any questions, drop me a line.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9a272a14-7cc1-4234-a4bc-8531ce7b8597" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/WatiN" rel="tag">WatiN</a>,<a href="http://technorati.com/tags/Jam+Session" rel="tag">Jam
Session</a>,<a href="http://technorati.com/tags/Coding+Exercise" rel="tag">Coding
Exercise</a>,<a href="http://technorati.com/tags/Learn+to+Code" rel="tag">Learn to
Code</a>,<a href="http://technorati.com/tags/Browser+Testing" rel="tag">Browser Testing</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=5a613524-5607-4fbb-a89f-74b5afb7c5e9" />
      </body>
      <title>Learn to Code WatiN: Browser Test your Web Site with WatiN</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,5a613524-5607-4fbb-a89f-74b5afb7c5e9.aspx</guid>
      <link>http://www.cptloadtest.com/2010/04/07/Learn-To-Code-WatiN-Browser-Test-Your-Web-Site-With-WatiN.aspx</link>
      <pubDate>Wed, 07 Apr 2010 15:27:53 GMT</pubDate>
      <description>&lt;p&gt;
Aligned with another jam session at Ann Arbor's &lt;a href="http://www.comejamwithus.org"&gt;Come
Jam With Us&lt;/a&gt; is another installment of Learn to Code, this time providing an introduction
to WatiN, or Web Application Testing in .NET. The jam session was held at the offices
of SRT Solutions in Ann Arbor, Michigan, at 5:30p, Tuesday April 6th. Though thunderstorms
were in the forecast, the predicted high was 72°F (22°C), so we weren't bothered by
the same 8&amp;quot; of fluffy white snow that caused cancellations and delays during
the my session on ASP.NET MVC 2. But for those that couldn't make the WatiN jam session,
might I recommend the exercise below.
&lt;/p&gt;
&lt;h3&gt;About This Exercise
&lt;/h3&gt;
&lt;p&gt;
This coding exercise is designed to give you an introduction to browser-based testing
using the WatiN framework, or Web Application Testing in .NET. The framework allows
developers to create integration tests (using a unit testing framework like MbUnit,
NUnit, or MSTest) to test and assert their application within a browser window. The
framework interacts with the browser DOM much like and end-user, producing reliable
results that mimic the real world. In this sample, we will write a few WatiN tests
against the Google search engine.
&lt;/p&gt;
&lt;h3&gt;Prerequisites
&lt;/h3&gt;
&lt;p&gt;
To complete this exercise, you will need to meet or complete a few prerequisites.
Please complete these prerequisites before moving on. The session is designed to be
completed in about an hour, but setup and prerequisites are not included in that time.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
An active internet connection. (Our tests will be conducted against live third-party
sites.) 
&lt;/li&gt;
&lt;li&gt;
Install Microsoft Visual Studio 2008 or Microsoft Visual Studio 2010. 
&lt;/li&gt;
&lt;li&gt;
&lt;a title="Download WATiN from SourceForge" href="http://sourceforge.net/projects/watin/files/"&gt;Download&lt;/a&gt; and
extract the latest version of the WatiN framework. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Exercise 0: Getting Started
&lt;/h3&gt;
&lt;h4&gt;Creating a Project
&lt;/h4&gt;
&lt;p&gt;
WatiN is generally used within the context of a unit testing framework. For this exercise,
we will be using a Visual Studio Test Project and MSTest to wrap our WatiN code.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new &amp;quot;Test Project&amp;quot; in Visual Studio named &amp;quot;WatinSample&amp;quot;.
The language is up to you, but all of the examples in this post will use C#. 
&lt;/li&gt;
&lt;li&gt;
Feel free to delete the Authoring Tests document, the Manual Test file, and UnitTest1.cs.
We won't be using these. 
&lt;/li&gt;
&lt;li&gt;
Add a reference to WatiN.Core.dll from the bin directory of your extracted WatiN download. 
&lt;/li&gt;
&lt;li&gt;
Compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Exercise 1: My First Browser Tests
&lt;/h3&gt;
&lt;p&gt;
In our first test, we will use the project we just created to test Google's home page.
After accessing &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;, we will
check a few properties of the browser and a few loaded elements to ensure that the
expected page was returned. The first thing we will need is a new Unit Test class
to start our testing.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new class (Right click on the &amp;quot;WatinSample&amp;quot; project and select
Add –&amp;gt; Class…), called WhenViewingTheGoogleHomePage. 
&lt;/li&gt;
&lt;li&gt;
Mark the class as public. 
&lt;/li&gt;
&lt;li&gt;
Add the MSTest [TestClass] attribute to the new class. 
&lt;/li&gt;
&lt;li&gt;
Compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingTheGoogleHomePage
  {
  }
}&lt;/pre&gt;
&lt;h4&gt;Make an instance of the browser
&lt;/h4&gt;
&lt;p&gt;
Now that we have a test class, we can start writing WatiN code. Each of our tests
will first need a Browser object to test against. Using methods attributed with TestInitialize
and TestCleanup, we can create a browser instance before the test starts and shut
it down when the test is complete.
&lt;/p&gt;
&lt;p&gt;
Creating an instance of a browser in WatiN is easy: simply create a new instance of
the IE class, passing in a URL. We can assign this new class to a field of type Browser,
which is a base class of all browser classes in WatiN. Currently, WatiN supports Internet
Explorer and Firefox.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a private field in the test class named browserInstance of type WatiN.Core.Browser.
Add a using statement to WatiN.Core if you wish. 
&lt;/li&gt;
&lt;li&gt;
Create a test initialization method named WithAnInstanceOfTheBrowser and give it the
[TestInitialize] attribute. Within this method, create a new instance of the IE class,
passing in the Google URL, &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;,
and assigning the instance to the browserInstance field. 
&lt;/li&gt;
&lt;li&gt;
Finally, create a test cleanup method named ShutdownBrowserWhenDone and give it the
[TestCleanup] attribute. Within this method, execute the Close() method on our browser
instance and assign the field to null to assist with object disposal. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingTheGoogleHomePage
  {
    Browser browserInstance;

    [TestInitialize]
    public void WithAnInstanceOfTheBrowser()
    {
      browserInstance = new IE(&amp;quot;http://www.google.com&amp;quot;);
    }

    [TestCleanup]
    public void ShutdownBrowserWhenDone()
    {
      browserInstance.Close();
      browserInstance = null;
    }
  }
}&lt;/pre&gt;
&lt;h4&gt;Our First Tests: Checking for existence of an element
&lt;/h4&gt;
&lt;p&gt;
There are three prominent items on the Google home page: the Google logo, the search
criteria text box, and the search button. Using WatiN, we can check for them all.
The WatiN Browser object contains an Elements collection, which is a flattened collection
of every element in the entire DOM. Like any collection, you can use Linq and lambda
expressions to search for items within this collection. Alternately, you may also
use the Element method, which accepts the same lambda expression that would be used
within the Where extension method on the collection, and returns the first or default
element. For more specific searches, WatiN's Browser object includes similar collections
and methods for searching explicitly for Images (&amp;lt;IMG&amp;gt;), Paras (&amp;lt;P&amp;gt;),
Text Fields (&amp;lt;INPUT type=&amp;quot;text&amp;quot; /&amp;gt;), and so on.
&lt;/p&gt;
&lt;p&gt;
On each returned Element (or derived Para, Image, or Text Field, etc., all of which
inherit from Element), WatiN supplies properties for accessing the CSS Class, Id,
InnerHtml, Name, Tag, Text, Value, or many other attributes. The method GetAttributeValue(string
attributeName) is provided for accessing other attributes that are not explicitly
defined on the object (uncommon attributes and custom attributes). Finally, elements
also contain a Style property, which not only gives access to the inline style attribute,
but also any CSS properties associated with the element from Internal Style (in the
Page Head) or External Style (in an external style sheet).
&lt;/p&gt;
&lt;p&gt;
On to checking for the three elements within the Google home page: the logo, the criteria
input, and the search button. First, check for the existence of the Google logo graphic.
The image can be found by searching the DOM for an image with an Id of &amp;quot;logo&amp;quot;.
WatiN works very closely with lambda expressions, so we can use these to help us find
out graphic.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public method named PageShouldContainGoogleLogo. 
&lt;/li&gt;
&lt;li&gt;
Add the MSTest [TestMethod] attribute to the method. 
&lt;/li&gt;
&lt;li&gt;
Search for and assert on the existence of an image with the Id of &amp;quot;logo&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Optionally, we can also check that the image has the expected Alt attribute; in this
case, the value should be &amp;quot;Google&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void PageShouldContainGoogleLogo()
{
  Image googleLogo;
  googleLogo = browserInstance.Image(img =&amp;gt; img.Id == &amp;quot;logo&amp;quot;);
  Assert.IsTrue(googleLogo.Exists);
  Assert.AreEqual(&amp;quot;Google&amp;quot;, googleLogo.Alt);
}&lt;/pre&gt;
&lt;p&gt;
Next, check for the existence of the search criteria input box. WatiN refers to these
elements as Text Fields, using the TextField type. Additionally, this form field is
identified by its Name rather than its Id. In Google, the name given to the criteria
input is &amp;quot;q&amp;quot;.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public method named PageShouldContainSearchCriteriaInput and give it
the [TestMethod] attribute. 
&lt;/li&gt;
&lt;li&gt;
Search for and assert on the existence of a Text Field with the name &amp;quot;q&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void PageShouldContainSearchCriteriaInput()
{
  TextField criteriaInput;
  criteriaInput = browserInstance.TextField(tf =&amp;gt; tf.Name == &amp;quot;q&amp;quot;);
  Assert.IsTrue(criteriaInput.Exists);
}&lt;/pre&gt;
&lt;p&gt;
Finally, check for the existence of the search button using the Button method. In
our lambda expression, it is not important to know if the field is identified by a
Name property or an Id attribute, as WatiN supplies a IdOrName property to help us
find the element. The value to identify the button is &amp;quot;btnG&amp;quot;.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public method named PageShouldContainSearchButton and give it the [TestMethod]
attribute. 
&lt;/li&gt;
&lt;li&gt;
Search for and assert on the existence of a Button with the Id or Name of 'btnG&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Optionally, we can also check the value of the button, which is the text displayed
on the button on-screen. This text should be &amp;quot;Google Search&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void PageShouldContainSearchButton()
{
  Button searchButton;
  searchButton = browserInstance.Button(btn =&amp;gt; btn.IdOrName == &amp;quot;btnG&amp;quot;);
  Assert.IsTrue(searchButton.Exists);
  Assert.AreEqual(&amp;quot;Google Search&amp;quot;, searchButton.Value);
}&lt;/pre&gt;
&lt;h4&gt;Working with Style
&lt;/h4&gt;
&lt;p&gt;
WatiN can access properties on the DOM beyond just Text values and Alt attributes.
WatiN also has full access to the style that CSS has applied to an element. Let's
check out a few CSS properties, both those explicitly defined by WatiN and those implicitly
accessible through the WatiN framework.
&lt;/p&gt;
&lt;p&gt;
For our first style check, we'll take a look at the default font family used on the
Google Home Page. Font Family is one of the explicitly available style properties
on a WatiN element. Some others, like Color, Display, and Height are also explicitly
defined.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public test method named BodyShouldUseArialFontFamily. 
&lt;/li&gt;
&lt;li&gt;
Assert that the font family assigned to the body matches &amp;quot;arial, sans-serif&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void BodyShouldUseArialFontFamily()
{
  Assert.AreEqual(&amp;quot;arial, sans-serif&amp;quot;, browserInstance.Body.Style.FontFamily);
}&lt;/pre&gt;
&lt;p&gt;
For our second style check, we will look for an implicit style definition. At the
top of the Google Home Page is a series of links to other areas of Google, such as
Images, Videos, Maps, and News. At the end of this list is a More link, that when
clicked, displays a hidden DIV tag containing even more links, such as Books, Finance,
and Google Translate. Since we do not have any code in our test initialization that
interacts with the browser, and thus nothing that is clicking the More link, that
DIV should still have a hidden visibility. However, since Visibility isn't an explicitly
defined style property within WatiN, we need to use the GetAttributeValue method to
retrieve the current visibility setting.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public test method named MoreItemsShouldNotBeVisibleOnPageLoad. 
&lt;/li&gt;
&lt;li&gt;
Search for the More Items DIV. It's Id is &amp;quot;gbi&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Using the property lookup method, GetAttributeValue(string attributeName), check that
the Visibility is set to &amp;quot;hidden&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void MoreItemsShouldNotBeVisibleOnPageLoad()
{
  var googleBarMoreItems = browserInstance.Div(gbi =&amp;gt; gbi.Id == &amp;quot;gbi&amp;quot;);
  Assert.AreEqual(&amp;quot;hidden&amp;quot;, googleBarMoreItems.Style.GetAttributeValue(&amp;quot;visibility&amp;quot;));
}&lt;/pre&gt;
&lt;h3&gt;Exercise 2: Interacting with the Browser
&lt;/h3&gt;
&lt;p&gt;
Browser Integration tests are more than just loading a page and checking a few element
attributes. Our tests may also need to enter values into form fields, click links
and buttons, or interact with browser navigation like the back button. WatiN fully
supports all of these features in a very intuitive fashion.
&lt;/p&gt;
&lt;h4&gt;A new test class, this time with Search Capability
&lt;/h4&gt;
&lt;p&gt;
Create a new test class, similar to what we did in Exercise 1, calling the new test
class WhenViewingGoogleSearchResultsForComeJamWithUs. Also add in the TestInitialize
and TestCleanup methods that open and close the browser. However, this time, after
we load &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;, enter a value into
the search criteria input and then click the Google Search button.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new class named WhenViewingGoogleSearchResultsForComeJamWithUs, similar to
what was done in Exercise 1. 
&lt;/li&gt;
&lt;li&gt;
Add in the TestInitialize and TestCleanup methods from Exercise 1. Name the Initialize
method WithAnInstanceOfTheBrowserSearchingGoogle. 
&lt;/li&gt;
&lt;li&gt;
After the code that initializes the IE class, find the search criteria Text Field
and set its value to &amp;quot;Come Jam With Us&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
After setting the Text Field value, click the Google Search button by calling the
Click() method on the Button class. 
&lt;/li&gt;
&lt;li&gt;
Compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace WatinSample
{
  [TestClass]
  public class WhenViewingGoogleSearchResultsForComeJamWithUs
  {
    Browser browserInstance;

    [TestInitialize]
    public void WithAnInstanceOfTheBrowserSearchingGoogle()
    {
      browserInstance = new IE(@&amp;quot;http://www.google.com&amp;quot;);
      TextField criteria =
        browserInstance.TextField(tf =&amp;gt; tf.Name == &amp;quot;q&amp;quot;);
      criteria.Value = &amp;quot;Come Jam With Us&amp;quot;;
      Button search =
        browserInstance.Button(btn =&amp;gt; btn.IdOrName == &amp;quot;btnG&amp;quot;);
      search.Click();
    }

    [TestCleanup]
    public void ShutdownBrowserWhenDone()
    {
      browserInstance.Close();
      browserInstance = null;
    }
  }
}&lt;/pre&gt;
&lt;p&gt;
With this code, or initialized test will load the Google Home Page and will conduct
a search for &amp;quot;Come Jam With Us&amp;quot;.
&lt;/p&gt;
&lt;h4&gt;Validating the Search Results Page
&lt;/h4&gt;
&lt;p&gt;
For our first verification, let's check the URL for the browser window. The search
result URL should contain the search criteria in the URL's query string; we can validate
this using the URL property on our instance of the Browser object.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public test method named BrowserUrlShouldContainSearchCriteria. 
&lt;/li&gt;
&lt;li&gt;
Validate that the current browser URL contains the search criteria information, &amp;quot;q=Come+Jam+With+Us&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void BrowserUrlShouldContainSearchCriteria()
{
  Assert.IsTrue(browserInstance.Url.Contains(@&amp;quot;q=Come+Jam+With+Us&amp;quot;));
}&lt;/pre&gt;
&lt;h4&gt;Finding Child Elements
&lt;/h4&gt;
&lt;p&gt;
With WatiN, we are not just limited to searching for items directly from the Browser
object. We can also search for child elements directly from their parent element or
any ancestor element. Our search results should contain a search result item linking
to the Come Jam With Us web site. The Google Results page contains a DIV identified
as &amp;quot;res&amp;quot; that serves as a container for all search result information. Rather
than checking that our Come Jam With Us link exists somewhere on the page, we should
search for it directly within the results DIV.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public test method named ResultsShouldContainLinkToComeJamWithUs. 
&lt;/li&gt;
&lt;li&gt;
From the browser instance, find a DIV identified as &amp;quot;res&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Assert that a link to &lt;a href="http://www.comejamwithus.org"&gt;http://www.comejamwithus.org&lt;/a&gt; exists
within the &amp;quot;res&amp;quot; DIV. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void ResultsShouldContainLinkToComeJamWithUs()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&amp;gt; div.IdOrName == &amp;quot;res&amp;quot;);
  comeJamWithUs =
    searchResults.Link(link =&amp;gt; link.Url == @&amp;quot;http://www.comejamwithus.org/&amp;quot;);
  Assert.IsTrue(comeJamWithUs.Exists);
}&lt;/pre&gt;
&lt;h4&gt;Inner Text verses InnerHtml
&lt;/h4&gt;
&lt;p&gt;
An element may contain many child elements. An anchor tag—&amp;lt;A href=&amp;quot;#&amp;quot;&amp;gt;—can
contain text, and child elements may make portions of that text bold, italic, underlined,
or even bright red. Through WatiN, we can access that inner content as straight text
without the formatting, or as the InnerHtml including all of the child elements.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create two public test methods, one named ResultsLinkContainsComeJamWithUsText and
the other named ResultsLinkContainsComeJamWithUsHtml. 
&lt;/li&gt;
&lt;li&gt;
In both methods, search for the results DIV, as we did in the previous test. 
&lt;/li&gt;
&lt;li&gt;
In both methods, search through the results DIV for a link with a URL matching &lt;a href="http://www.comejamwithus.org"&gt;http://www.comejamwithus.org&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
In the Text method, assert that the text of the link matches &amp;quot;Come Jam with us
(Software Development Study Group)&amp;quot;. Note that the value contains no child HTML
elements. 
&lt;/li&gt;
&lt;li&gt;
In the HTML method, assert that the InnerHtml of the link matches &amp;quot;&amp;lt;EM&amp;gt;Come
Jam with us&amp;lt;/EM&amp;gt; (Software Development Study Group)&amp;quot;. Note that for the
same link, we now have the emphasis tags surrounding Come Jam With Us. 
&lt;/li&gt;
&lt;li&gt;
Compile and run both tests. The tests should pass.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void ResultsLinkContainsComeJamWithUsText()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&amp;gt; div.IdOrName == &amp;quot;res&amp;quot;);
  comeJamWithUs =
    searchResults.Link(link =&amp;gt; link.Url == @&amp;quot;http://www.comejamwithus.org/&amp;quot;);
  Assert.AreEqual(@&amp;quot;Come Jam with us (Software Development Study Group)&amp;quot;,
    comeJamWithUs.Text);
}

[TestMethod]
public void ResultsLinkContainsComeJamWithUsHtml()
{
  Link comeJamWithUs;
  Div searchResults = browserInstance.Div(div =&amp;gt; div.IdOrName == &amp;quot;res&amp;quot;);
  comeJamWithUs =
    searchResults.Link(link =&amp;gt; link.Url == @&amp;quot;http://www.comejamwithus.org/&amp;quot;);
  Assert.AreEqual(
    @&amp;quot;&amp;lt;EM&amp;gt;Come Jam with us&amp;lt;/EM&amp;gt; (Software Development Study Group)&amp;quot;,
    comeJamWithUs.InnerHtml);
}&lt;/pre&gt;
&lt;h4&gt;Back to the Start
&lt;/h4&gt;
&lt;p&gt;
As previously mentioned, we can also fully interact with the browser, itself. Our
test initialization started from the Google Home Page and performed a search. Using
functionality built in to WatiN, we can execute the browser's back navigation to return
to the previous page.
&lt;/p&gt;
&lt;p&gt;
For our next test, execute a back navigation and verify that the browser's URL matches &lt;a href="http://www.google.com/"&gt;http://www.google.com/&lt;/a&gt;.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a public test method named PageShouldHaveComeFromGoogleDotCom. 
&lt;/li&gt;
&lt;li&gt;
Execute back navigation in the browser by calling the Back() method on browserInstance. 
&lt;/li&gt;
&lt;li&gt;
Validate that the browser URL matches &lt;a href="http://www.google.com/"&gt;http://www.google.com/&lt;/a&gt;. 
&lt;/li&gt;
&lt;li&gt;
Compile and run the test. The test should pass.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void PageShouldHaveComeFromGoogleDotCom()
{
  string previousUrl;
  browserInstance.Back();
  previousUrl = browserInstance.Url;
  Assert.AreEqual(@&amp;quot;http://www.google.com/&amp;quot;, previousUrl);
}&lt;/pre&gt;
&lt;h4&gt;Putting it all together
&lt;/h4&gt;
&lt;p&gt;
Some interactions on a page cause element properties to change. An example of this
is the More link from Exercise 1; when the end-user clicks the More link, the More
Items DIV appears because the link's click event changes the Visibility style property
of the DIV to visible. For our final test, we will use what we have learned to test
this functionality.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new public test method named MoreItemsShouldBeVisibleOnMoreLinkClick.&lt;/li&gt;
&lt;li&gt;
Search for the header bar of Google links, a DIV with an Id of &amp;quot;gbar&amp;quot;.&lt;/li&gt;
&lt;li&gt;
Within &amp;quot;gbar&amp;quot;, search for the More Items DIV by an Id or Name of &amp;quot;gbi&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Assert that the visibility style property has a value of &amp;quot;hidden&amp;quot;. 
&lt;/li&gt;
&lt;li&gt;
Within &amp;quot;gbar&amp;quot;, search for the More link by its class name, &amp;quot;gb3&amp;quot;.
Note that since a class attribute may contain multiple class definitions, this is
accomplished by validating that the class attribute contains the class you are searching
for. 
&lt;/li&gt;
&lt;li&gt;
Execute a Click event on the link. 
&lt;/li&gt;
&lt;li&gt;
Assert that the visibility style property of the More Items DIV has changed to &amp;quot;visible&amp;quot;. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;[TestMethod]
public void MoreItemsShouldBeVisibleOnMoreLinkClick()
{
  var googleBar = browserInstance.Div(gbar =&amp;gt; gbar.Id == &amp;quot;gbar&amp;quot;);
  var googleBarMoreItems = googleBar.Div(gbi =&amp;gt; gbi.Id == &amp;quot;gbi&amp;quot;);
  Assert.AreEqual(&amp;quot;hidden&amp;quot;,
    googleBarMoreItems.Style.GetAttributeValue(&amp;quot;visibility&amp;quot;));
  var googleBarMoreLink =
    googleBar.Link(link =&amp;gt; link.ClassName.Contains(&amp;quot;gb3&amp;quot;));
  googleBarMoreLink.Click();
  Assert.AreEqual(&amp;quot;visible&amp;quot;,
    googleBarMoreItems.Style.GetAttributeValue(&amp;quot;visibility&amp;quot;));
}&lt;/pre&gt;
&lt;h4&gt;That's It
&lt;/h4&gt;
&lt;p&gt;
Now that we have spent some time on basic properties, interactions, and style sheets
within the WatiN framework, hopefully you can apply this to your own application and
get started with your own browser-based integration tests. If you would like more
information, I encourage you to check out the WatiN site at &lt;a href="http://watin.sourceforge.net"&gt;http://watin.sourceforge.net&lt;/a&gt;.
And as always, if you have any questions, drop me a line.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9a272a14-7cc1-4234-a4bc-8531ce7b8597" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/WatiN" rel="tag"&gt;WatiN&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Jam+Session" rel="tag"&gt;Jam
Session&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Coding+Exercise" rel="tag"&gt;Coding
Exercise&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Learn+to+Code" rel="tag"&gt;Learn to
Code&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Browser+Testing" rel="tag"&gt;Browser Testing&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=5a613524-5607-4fbb-a89f-74b5afb7c5e9" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,5a613524-5607-4fbb-a89f-74b5afb7c5e9.aspx</comments>
      <category>Learn to Code</category>
      <category>Testing</category>
      <category>Tools</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=4faf4135-777f-41e2-9239-dce95df5e2a7</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=4faf4135-777f-41e2-9239-dce95df5e2a7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
</p>
        <blockquote>
          <p style="color: maroon">
            <em>The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.</em>
          </p>
        </blockquote>
        <p>
The following solution applies to the ASP.NET MVC 2 framework. If you are looking
for a solution in the first version of the ASP.NET MVC framework, try last November's
post on <a href="http://www.cptloadtest.com/2009/12/01/Validating-ASPNET-MVC-Route-Values-With-ActionFilterAttribute.aspx">Validating
ASP.NET MVC Route Values with ActionFilterAttribute</a>.
</p>
        <p>
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is an optional URL Parameter (UrlParameter.Optional),
as defined in the default routing tables, but the value can be parsed to other types
such as an integer for a default Details action. The problem stems from when invalid
values or missing values are passed to these integer-expecting actions; MVC handles
the inability to parse the value into an integer, but then throws an exception trying
to pass a null value to a Controller Action expecting a value type for a method argument.
</p>
        <p>
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
</p>
        <p>
Under ASP.NET MVC 2, the solution lies with a DefaultValueAttribute. Prior to executing
a Controller Action, the DefaultValueAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this allows me to keep that method argument as a value-type integer and avoid
Nullable&lt;int&gt;.HasValue.
</p>
        <p>
The attribute is a member of the System.ComponentModel namespace, and accepts any
one of many different base values as the default to assign to the route value should
the parsing fail. Unlike the ActionFilterAttribute used to solve this problem in the
first version of the ASP.NET MVC framework, there are no route redirects, which also
means we do not modify the browser URL. (Using the ActionFilterAttribute, the browser
location is redirected from ~/Widgets/Details/ or ~/Widgets/Details/Foo to ~/Widgets/Details/0,
but with the DefaultValueAttribute, no such redirect occurs.)
</p>
        <h3>Usage
</h3>
        <pre class="csharp:nocontrols" name="code">public class WidgetsController : Controller
{
  public ActionResult Details([DefaultValue(0) int id)
  {
    return View();
  }
}</pre>
        <p>
There is very little code to make this all happen. With one attribute added to the
method argument, MVC validates that my identity is an integer or otherwise provides
a replacement default value. I can depend on my user input without having to laden
my code with unnecessary value checks and without risk of unhandled exceptions. However,
the DefaultValueAttribute is just for providing a default value for your value-type
method arguments. Unlike the ActionFilterAttribute, the DefaultValueAttribute will
not perform any filtering, such as making sure a string argument begins with "Foo"
or that a decimal input only contains two decimal places; for this type of logic,
continue to use the ActionFilterAttribute. DefaultValueAttribute is a perfect fit
for eliminating Nullable&lt;int&gt; in Action arguments, making the code clean, simple,
and elegant. Eliminate the extra code, and let the framework do the work for you.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a0a44014-ffba-4b02-b952-0ae0181262d7" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/DefaultFilterAttribute" rel="tag">DefaultFilterAttribute</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4faf4135-777f-41e2-9239-dce95df5e2a7" />
      </body>
      <title>Validating ASP.NET MVC 2 Route Values with DefaultValueAttribute</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</guid>
      <link>http://www.cptloadtest.com/2010/04/05/Validating-ASPNET-MVC-2-Route-Values-With-DefaultValueAttribute.aspx</link>
      <pubDate>Mon, 05 Apr 2010 02:08:03 GMT</pubDate>
      <description>&lt;p&gt;
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p style="color: maroon"&gt;
&lt;em&gt;The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The following solution applies to the ASP.NET MVC 2 framework. If you are looking
for a solution in the first version of the ASP.NET MVC framework, try last November's
post on &lt;a href="http://www.cptloadtest.com/2009/12/01/Validating-ASPNET-MVC-Route-Values-With-ActionFilterAttribute.aspx"&gt;Validating
ASP.NET MVC Route Values with ActionFilterAttribute&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is an optional URL Parameter (UrlParameter.Optional),
as defined in the default routing tables, but the value can be parsed to other types
such as an integer for a default Details action. The problem stems from when invalid
values or missing values are passed to these integer-expecting actions; MVC handles
the inability to parse the value into an integer, but then throws an exception trying
to pass a null value to a Controller Action expecting a value type for a method argument.
&lt;/p&gt;
&lt;p&gt;
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
&lt;/p&gt;
&lt;p&gt;
Under ASP.NET MVC 2, the solution lies with a DefaultValueAttribute. Prior to executing
a Controller Action, the DefaultValueAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this allows me to keep that method argument as a value-type integer and avoid
Nullable&amp;lt;int&amp;gt;.HasValue.
&lt;/p&gt;
&lt;p&gt;
The attribute is a member of the System.ComponentModel namespace, and accepts any
one of many different base values as the default to assign to the route value should
the parsing fail. Unlike the ActionFilterAttribute used to solve this problem in the
first version of the ASP.NET MVC framework, there are no route redirects, which also
means we do not modify the browser URL. (Using the ActionFilterAttribute, the browser
location is redirected from ~/Widgets/Details/ or ~/Widgets/Details/Foo to ~/Widgets/Details/0,
but with the DefaultValueAttribute, no such redirect occurs.)
&lt;/p&gt;
&lt;h3&gt;Usage
&lt;/h3&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public class WidgetsController : Controller
{
  public ActionResult Details([DefaultValue(0) int id)
  {
    return View();
  }
}&lt;/pre&gt;
&lt;p&gt;
There is very little code to make this all happen. With one attribute added to the
method argument, MVC validates that my identity is an integer or otherwise provides
a replacement default value. I can depend on my user input without having to laden
my code with unnecessary value checks and without risk of unhandled exceptions. However,
the DefaultValueAttribute is just for providing a default value for your value-type
method arguments. Unlike the ActionFilterAttribute, the DefaultValueAttribute will
not perform any filtering, such as making sure a string argument begins with &amp;quot;Foo&amp;quot;
or that a decimal input only contains two decimal places; for this type of logic,
continue to use the ActionFilterAttribute. DefaultValueAttribute is a perfect fit
for eliminating Nullable&amp;lt;int&amp;gt; in Action arguments, making the code clean, simple,
and elegant. Eliminate the extra code, and let the framework do the work for you.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a0a44014-ffba-4b02-b952-0ae0181262d7" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DefaultFilterAttribute" rel="tag"&gt;DefaultFilterAttribute&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4faf4135-777f-41e2-9239-dce95df5e2a7" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</comments>
      <category>ASP.Net</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=6682b44d-7660-4b5e-8746-bdcfdae81f84</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6682b44d-7660-4b5e-8746-bdcfdae81f84</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The previous installments of this series cover the core events of the ASP.NET Page
class, of the ASP.NET Controls, and how the page and controls work together to co-exist
in the same sandbox. We've gotten to the point where we know how these controls will
interact with the page so that we can make our page more than just a sea of crazy
peach gradient backgrounds, but that still isn't enough. Static content is so 1999,
and that party broke up long ago. The next step in a quality, modern web site is creating
dynamic content, and rendering it to the page. To do this, we need Data Binding.
</p>
        <blockquote>
          <h3>About the Series
</h3>
          <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
          <p>
Part 1: <a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Events
of the ASP.NET Page Life Cycle</a><br />
Part 2: <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">ASP.NET
Page &amp; WebControl Event Execution Order</a><br />
Part 3: Getting Started with ASP.NET Data Binding 
<br />
Part 4: <a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">Wiring
Events to your Page</a></p>
        </blockquote>
        <h3>Getting Started with ASP.NET Data Binding
</h3>
        <p>
When we load up data from the database, we could use a for-each loop to iterate through
the data, manually create every new control, such as a table row, and manually write
our data as the content for each new control, but that would be a lot of useless,
repetitive, and senseless <a title="Boilerplate Code (Wikipedia)" href="http://en.wikipedia.org/wiki/Boilerplate_(text)#Boilerplate_code">boilerplate
code</a>. Instead, we should use functionality that is built in to the ASP.NET framework,
and allow the framework to do a lot of the work for us. This process of using the
framework for binding dynamic data to the page, or to controls within the page, is
called Data Binding.
</p>
        <p>
As we've discussed before, there are several events that handle the processing and
rendering of a page. Similarly, there are several events that handle the process of
binding data to a page (and its child controls). Each of these events handle a very
specific sub-set of the process. And as before, knowing the execution order of these
events will make you more proficient at ASP.NET development.
</p>
        <p>
For our examples, we will have a page for displaying Company data. The page will host
a TextBox control containing the name of the company, and a repeater for departments
within the company. Each repeater item will display the name of the department and
a DropDownList containing the names of employees within the department.
</p>
        <pre class="csharp:nocontrols" name="code">public partial class CompanyInformation : Page
{
  private Repeater _deparmentsRepeater;
  private TextBox _companyNameTextbox;

  public _Default()
  {
    Init += Page_Init;
    Load += Page_Load;
  }

  void Page_Init(object sender, EventArgs e)
  {
    var container = new Panel();
    _companyNameTextbox = new TextBox();
    _companyNameTextbox.ID = "companyNameTextbox";
    _companyNameTextbox.DataBinding += TBox_DataBinding;
    container.Controls.Add(_companyNameTextbox);
    myForm.Controls.Add(container);

    _deparmentsRepeater = new Repeater();
    _deparmentsRepeater.ID = "departmentsRepeater";
    _deparmentsRepeater.ItemCreated += Rpt_ItemCreated;
    _deparmentsRepeater.ItemDataBound += Rpt_ItemDataBound;
    myForm.Controls.Add(_deparmentsRepeater);
  }
}</pre>
        <h3>ASP.NET Data Binding Events
</h3>
        <h4>Page.DataBind() / Control.DataBind()
</h4>
        <p>
The first "Event" of ASP.NET's Data Binding Life Cycle is not an event at
all; it is a method that gets everything started. The DataBind method initiates the
process of binding all controls on the page, attaching your dynamic loaded classes
or lists to the areas of your page where the data will be displayed. DataBind can
be executed at the Page level, or on any bindable Control. Whenever it is executed,
DataBind also cascades through all child controls of the execution point. If you have
an ASP.NET Panel Control that contains several TextBox controls, executing DataBind
on the panel will run the method on the Panel first, then all of the child text boxes.
If you execute on the Page, it will fire first on the page, then on the panel, then
on all of the text boxes. The cascading rules are the same as the event execution
order discussed in <a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx" target="_blank">Part
2</a> of this series.
</p>
        <p>
Traditionally, DataBind is executed from within the Page Load event, though it can
be executed from other places instead, as long as you keep the rest of the page life
cycle in mind. For example, do not run DataBind on your page in PreInit; none of your
controls have been instantiated, yet. Also, as my personal preference, I put all of
my data retrieval logic in an override of the DataBind method, keeping all of my binding
logic in a centralized location, though this can also occur elsewhere providing that
it is prior to the execution of the base Page.DataBind() or Control.DataBind() method.
</p>
        <pre class="csharp:nocontrols" name="code">void Page_Load(object sender, EventArgs e)
{
  DataBind();
}
public override void DataBind()
{
  Trace.Write("Beginning to bind all controls");
  _myRepeater.DataSource = MyCompany.Departments();
  base.DataBind();
}</pre>
        <p>
Here, the Page Load event kicks off our override of the DataBind method. This override
retrieves a list of Departments for the company, and assigns the required properties
from the company class instance to controls as needed, which includes all controls
with a DataSource property. DataSource is usually a collection of objects to be used
for binding collections to list-based controls from DropDownLists to Repeaters, though
some other controls may have different needs and uses for DataSource. Above, we assign
the Departments list to the repeater's DataSource property, so that later on in the
binding process, the repeater can pass appropriate Department information on to each
of its RepeaterItems and child controls. With the DataSource defined, the base DataBind
method is executed to initiate binding to all controls on the page.
</p>
        <h4>DataBinding Event
</h4>
        <p>
DataBinding is the first actual event that fires in the DataBinding series, and it
will fire for every bindable control and child control from the point where the DataBind
method was executed, top-down. When the event executes, binding has not yet been executed,
but the data to be bound is available for manipulation. As such, binding has also
not yet executed on any of the child controls of the event target. With the exception
of list-based container controls like a DataGrid, GridView, or Repeater, during the
DataBinding event would be where any dynamic child controls are created and their
DataSources assigned.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _companyNameTextbox.DataBinding += TBox_DataBinding;
void TBox_DataBinding(object sender, EventArgs e)
{
  ((TextBox) sender).Text = MyCompany.Name;
}</pre>
        <p>
Above, we bind our TextBox control to the name of our company. TextBox does not have
a DataSource property, so the company name is manually assigned directly to the TextBox's
Text property.
</p>
        <h4>RowCreated / ItemCreated Event
</h4>
        <p>
Note: RowCreated is used for GridView Control, only. ItemCreated is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
<br />
The RowCreated and ItemCreated events are very similar in nature to the DataBinding
event, and are used for list-based container controls like a DataGrid, GridView, or
Repeater. This event will execute once for each item in the DataSource collection,
and correspond to one Row or Item in the control. On execution, the individual Row
or Item in your control has been created, any child controls specified in your markup
have been initialized and loaded, and the individual data item is available for manipulation.
However, Data Binding has not yet been executed on the row or any of its child controls.
This event cannot be dependent upon any control data since the binding has yet to
occur. An example scenario that would use this event would be if your data item contained
another list, such as our Department class instance that contains a list of Employees,
and the Employees will display in a dropdown. During Created, the dropdown's DataSource
can be assigned to the list contained within the Employees property. After the Created
event finished for the Row or Item, ASP.NET will automatically execute the DataBind
method on the Employees dropdown, binding the list data to populate the control. This
automatic execution of DataBind applies to all child controls of the Row or Item,
including controls defined in the Code-In-Front markup or those that were manually
created in the code-behind.
</p>
        <p>
Be aware that any code in the Created events cannot be dependent upon Control data.
Even if you specify the DataSource property of a child dropdown, the DropDownList.Items
list property is still empty. Executing code dependant upon control data, such as
setting the selected item, will fail.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _myRepeater.ItemCreated += Rpt_ItemCreated;
void Rpt_ItemCreated(object sender, RepeaterItemEventArgs e)
{
  //Preparing another Repeater Item for binding
  var dataItem = (Department) e.Item.DataItem;

  var departmentLabel = new Label();
  departmentLabel.Text = dataItem.Name;
  e.Item.Controls.Add(departmentLabel);

  var dropdownOfEmployees = new DropDownList();
  dropdownOfEmployees.DataSource = dataItem.Employees;
  dropdownOfEmployees.DataTextField = "Name";
  e.Item.Controls.Add(dropdownOfEmployees);

  e.Item.Controls.Add(new LiteralControl("<br />
")); }</pre>
        <p>
The ItemCreated event creates a label for the name of the department, assigning the
name, and a DropDownList of Employees, assigning only the list's DataSource. ItemCreated
is repeated for every item in the repeater's DataSource collection, or in other words,
every department associated with our company. Because DataBinding has not yet executed
for any child control, none of the Employee ListItems exist in our DropDownList, but
because it has not yet executed, we also do not need to manually create the ListItems
or manually re-execute DropDownList.DataBind().
</p>
        <h4>RowDataBound / ItemDataBound
</h4>
        <p>
Note: RowDataBound is used for GridView Control, only. ItemDataBound is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
<br />
The final event in the Data Binding Life Cycle is the Bound events, RowDataBound and
ItemDataBound. This event fires when the data binding process has completed for all
child controls within the Row or Item. All controls within the row have their data,
and code that is dependant upon control data, such as setting a selected item, can
now be executed. It is strongly advised that this event contains no modifications
to the item data that would require a DataBind method to be executed; this sort of
data manipulation should occur instead in the Created events, prior to Data Binding
the child control tree. If DataBind were to be executed again on this control, the
Data Binding process for this control and all children is restarted and re-executed,
causing controls to be Data Bound two or more times and potentially leading to an
unknown state and adverse outcomes in your application.
</p>
        <p>
The DataBound events indicate that binding has been completed for this item and all
child controls; for us, this means our DropDownList of employees has been fully bound,
and that a ListItem exists for each Employee. We can use this post-binding event to
perform any data-dependent logic. In this case, we now set the SelectedIndex of our
DropDownList; the method would fail if it was executed during ItemCreated. As with
ItemCreated, this event will execute for each item in our repeater's DataSource collection.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _myRepeater.ItemDataBound += Rpt_ItemDataBound;
void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  var dropdownOfEmployees = (DropDownList) e.Item.Controls[1];
  dropdownOfEmployees.SelectedIndex = 2;
}</pre>
        <h3>Proper Data Binding
</h3>
        <p>
This is the essence of Data Binding within ASP.NET Web Forms: one method and three
events. When done properly, the DataBind method should only be called once on a control
tree. Execute DataBind directly on the page, or execute it once on individual controls
such as each of your top-level Repeaters, but whatever path you follow, make sure
that the method is not executed twice on any single control. Use the binding events
to make this happen. Failure to do so can cause unfortunate side effects and extensive
pain.
</p>
        <ul>
          <li>
DataSources should be assigned prior to execution of a control's DataBind method. 
</li>
          <li>
Controls without a DataSource property can be manually bound using the controls DataBinding
event, after executing DataBind. 
</li>
          <li>
For collection-based container controls, define child controls and assign their DataSource
using the RowCreated/ItemCreated events. 
</li>
          <li>
For collection-based container controls, perform data-dependent logic on child controls
in the RowDataBound/ItemDataBound event. 
</li>
        </ul>
        <p>
The ASP.NET Page Life Cycle may seem like a monotonous mess, but it is a useful tool
if you understand the events, their order, and their relationship to each other. If
you missed them, go back and review <a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Part
1</a> for the core Page Life Cycle events and to <a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">Part
2</a> for the Event Execution Order between a page and its controls. Once you have
these down, there still are some oddities to look out for (and some tricks to help
you out). <a title="Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]" href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">As
the series continues</a>, we discuss some of these tips, tricks, and traps to help
you unleash the full power of your ASP.NET applications.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:233f1d3b-6345-4d60-a6cb-b67b55265237" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a>,<a href="http://technorati.com/tags/Data+Binding" rel="tag">Data Binding</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6682b44d-7660-4b5e-8746-bdcfdae81f84" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 3 [Data Binding]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</guid>
      <link>http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx</link>
      <pubDate>Wed, 24 Mar 2010 00:32:53 GMT</pubDate>
      <description>&lt;p&gt;
The previous installments of this series cover the core events of the ASP.NET Page
class, of the ASP.NET Controls, and how the page and controls work together to co-exist
in the same sandbox. We've gotten to the point where we know how these controls will
interact with the page so that we can make our page more than just a sea of crazy
peach gradient backgrounds, but that still isn't enough. Static content is so 1999,
and that party broke up long ago. The next step in a quality, modern web site is creating
dynamic content, and rendering it to the page. To do this, we need Data Binding.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;About the Series
&lt;/h3&gt;
&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;p&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Events
of the ASP.NET Page Life Cycle&lt;/a&gt; 
&lt;br /&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;ASP.NET
Page &amp;amp; WebControl Event Execution Order&lt;/a&gt; 
&lt;br /&gt;
Part 3: Getting Started with ASP.NET Data Binding 
&lt;br /&gt;
Part 4: &lt;a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;Wiring
Events to your Page&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h3&gt;Getting Started with ASP.NET Data Binding
&lt;/h3&gt;
&lt;p&gt;
When we load up data from the database, we could use a for-each loop to iterate through
the data, manually create every new control, such as a table row, and manually write
our data as the content for each new control, but that would be a lot of useless,
repetitive, and senseless &lt;a title="Boilerplate Code (Wikipedia)" href="http://en.wikipedia.org/wiki/Boilerplate_(text)#Boilerplate_code"&gt;boilerplate
code&lt;/a&gt;. Instead, we should use functionality that is built in to the ASP.NET framework,
and allow the framework to do a lot of the work for us. This process of using the
framework for binding dynamic data to the page, or to controls within the page, is
called Data Binding.
&lt;/p&gt;
&lt;p&gt;
As we've discussed before, there are several events that handle the processing and
rendering of a page. Similarly, there are several events that handle the process of
binding data to a page (and its child controls). Each of these events handle a very
specific sub-set of the process. And as before, knowing the execution order of these
events will make you more proficient at ASP.NET development.
&lt;/p&gt;
&lt;p&gt;
For our examples, we will have a page for displaying Company data. The page will host
a TextBox control containing the name of the company, and a repeater for departments
within the company. Each repeater item will display the name of the department and
a DropDownList containing the names of employees within the department.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public partial class CompanyInformation : Page
{
  private Repeater _deparmentsRepeater;
  private TextBox _companyNameTextbox;

  public _Default()
  {
    Init += Page_Init;
    Load += Page_Load;
  }

  void Page_Init(object sender, EventArgs e)
  {
    var container = new Panel();
    _companyNameTextbox = new TextBox();
    _companyNameTextbox.ID = &amp;quot;companyNameTextbox&amp;quot;;
    _companyNameTextbox.DataBinding += TBox_DataBinding;
    container.Controls.Add(_companyNameTextbox);
    myForm.Controls.Add(container);

    _deparmentsRepeater = new Repeater();
    _deparmentsRepeater.ID = &amp;quot;departmentsRepeater&amp;quot;;
    _deparmentsRepeater.ItemCreated += Rpt_ItemCreated;
    _deparmentsRepeater.ItemDataBound += Rpt_ItemDataBound;
    myForm.Controls.Add(_deparmentsRepeater);
  }
}&lt;/pre&gt;
&lt;h3&gt;ASP.NET Data Binding Events
&lt;/h3&gt;
&lt;h4&gt;Page.DataBind() / Control.DataBind()
&lt;/h4&gt;
&lt;p&gt;
The first &amp;quot;Event&amp;quot; of ASP.NET's Data Binding Life Cycle is not an event at
all; it is a method that gets everything started. The DataBind method initiates the
process of binding all controls on the page, attaching your dynamic loaded classes
or lists to the areas of your page where the data will be displayed. DataBind can
be executed at the Page level, or on any bindable Control. Whenever it is executed,
DataBind also cascades through all child controls of the execution point. If you have
an ASP.NET Panel Control that contains several TextBox controls, executing DataBind
on the panel will run the method on the Panel first, then all of the child text boxes.
If you execute on the Page, it will fire first on the page, then on the panel, then
on all of the text boxes. The cascading rules are the same as the event execution
order discussed in &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx" target="_blank"&gt;Part
2&lt;/a&gt; of this series.
&lt;/p&gt;
&lt;p&gt;
Traditionally, DataBind is executed from within the Page Load event, though it can
be executed from other places instead, as long as you keep the rest of the page life
cycle in mind. For example, do not run DataBind on your page in PreInit; none of your
controls have been instantiated, yet. Also, as my personal preference, I put all of
my data retrieval logic in an override of the DataBind method, keeping all of my binding
logic in a centralized location, though this can also occur elsewhere providing that
it is prior to the execution of the base Page.DataBind() or Control.DataBind() method.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;void Page_Load(object sender, EventArgs e)
{
  DataBind();
}
public override void DataBind()
{
  Trace.Write(&amp;quot;Beginning to bind all controls&amp;quot;);
  _myRepeater.DataSource = MyCompany.Departments();
  base.DataBind();
}&lt;/pre&gt;
&lt;p&gt;
Here, the Page Load event kicks off our override of the DataBind method. This override
retrieves a list of Departments for the company, and assigns the required properties
from the company class instance to controls as needed, which includes all controls
with a DataSource property. DataSource is usually a collection of objects to be used
for binding collections to list-based controls from DropDownLists to Repeaters, though
some other controls may have different needs and uses for DataSource. Above, we assign
the Departments list to the repeater's DataSource property, so that later on in the
binding process, the repeater can pass appropriate Department information on to each
of its RepeaterItems and child controls. With the DataSource defined, the base DataBind
method is executed to initiate binding to all controls on the page.
&lt;/p&gt;
&lt;h4&gt;DataBinding Event
&lt;/h4&gt;
&lt;p&gt;
DataBinding is the first actual event that fires in the DataBinding series, and it
will fire for every bindable control and child control from the point where the DataBind
method was executed, top-down. When the event executes, binding has not yet been executed,
but the data to be bound is available for manipulation. As such, binding has also
not yet executed on any of the child controls of the event target. With the exception
of list-based container controls like a DataGrid, GridView, or Repeater, during the
DataBinding event would be where any dynamic child controls are created and their
DataSources assigned.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _companyNameTextbox.DataBinding += TBox_DataBinding;
void TBox_DataBinding(object sender, EventArgs e)
{
  ((TextBox) sender).Text = MyCompany.Name;
}&lt;/pre&gt;
&lt;p&gt;
Above, we bind our TextBox control to the name of our company. TextBox does not have
a DataSource property, so the company name is manually assigned directly to the TextBox's
Text property.
&lt;/p&gt;
&lt;h4&gt;RowCreated / ItemCreated Event
&lt;/h4&gt;
&lt;p&gt;
Note: RowCreated is used for GridView Control, only. ItemCreated is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
&lt;br /&gt;
The RowCreated and ItemCreated events are very similar in nature to the DataBinding
event, and are used for list-based container controls like a DataGrid, GridView, or
Repeater. This event will execute once for each item in the DataSource collection,
and correspond to one Row or Item in the control. On execution, the individual Row
or Item in your control has been created, any child controls specified in your markup
have been initialized and loaded, and the individual data item is available for manipulation.
However, Data Binding has not yet been executed on the row or any of its child controls.
This event cannot be dependent upon any control data since the binding has yet to
occur. An example scenario that would use this event would be if your data item contained
another list, such as our Department class instance that contains a list of Employees,
and the Employees will display in a dropdown. During Created, the dropdown's DataSource
can be assigned to the list contained within the Employees property. After the Created
event finished for the Row or Item, ASP.NET will automatically execute the DataBind
method on the Employees dropdown, binding the list data to populate the control. This
automatic execution of DataBind applies to all child controls of the Row or Item,
including controls defined in the Code-In-Front markup or those that were manually
created in the code-behind.
&lt;/p&gt;
&lt;p&gt;
Be aware that any code in the Created events cannot be dependent upon Control data.
Even if you specify the DataSource property of a child dropdown, the DropDownList.Items
list property is still empty. Executing code dependant upon control data, such as
setting the selected item, will fail.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _myRepeater.ItemCreated += Rpt_ItemCreated;
void Rpt_ItemCreated(object sender, RepeaterItemEventArgs e)
{
  //Preparing another Repeater Item for binding
  var dataItem = (Department) e.Item.DataItem;

  var departmentLabel = new Label();
  departmentLabel.Text = dataItem.Name;
  e.Item.Controls.Add(departmentLabel);

  var dropdownOfEmployees = new DropDownList();
  dropdownOfEmployees.DataSource = dataItem.Employees;
  dropdownOfEmployees.DataTextField = &amp;quot;Name&amp;quot;;
  e.Item.Controls.Add(dropdownOfEmployees);

  e.Item.Controls.Add(new LiteralControl(&amp;quot;&lt;br /&gt;
&amp;quot;)); }&lt;/pre&gt;
&lt;p&gt;
The ItemCreated event creates a label for the name of the department, assigning the
name, and a DropDownList of Employees, assigning only the list's DataSource. ItemCreated
is repeated for every item in the repeater's DataSource collection, or in other words,
every department associated with our company. Because DataBinding has not yet executed
for any child control, none of the Employee ListItems exist in our DropDownList, but
because it has not yet executed, we also do not need to manually create the ListItems
or manually re-execute DropDownList.DataBind().
&lt;/p&gt;
&lt;h4&gt;RowDataBound / ItemDataBound
&lt;/h4&gt;
&lt;p&gt;
Note: RowDataBound is used for GridView Control, only. ItemDataBound is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
&lt;br /&gt;
The final event in the Data Binding Life Cycle is the Bound events, RowDataBound and
ItemDataBound. This event fires when the data binding process has completed for all
child controls within the Row or Item. All controls within the row have their data,
and code that is dependant upon control data, such as setting a selected item, can
now be executed. It is strongly advised that this event contains no modifications
to the item data that would require a DataBind method to be executed; this sort of
data manipulation should occur instead in the Created events, prior to Data Binding
the child control tree. If DataBind were to be executed again on this control, the
Data Binding process for this control and all children is restarted and re-executed,
causing controls to be Data Bound two or more times and potentially leading to an
unknown state and adverse outcomes in your application.
&lt;/p&gt;
&lt;p&gt;
The DataBound events indicate that binding has been completed for this item and all
child controls; for us, this means our DropDownList of employees has been fully bound,
and that a ListItem exists for each Employee. We can use this post-binding event to
perform any data-dependent logic. In this case, we now set the SelectedIndex of our
DropDownList; the method would fail if it was executed during ItemCreated. As with
ItemCreated, this event will execute for each item in our repeater's DataSource collection.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _myRepeater.ItemDataBound += Rpt_ItemDataBound;
void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  var dropdownOfEmployees = (DropDownList) e.Item.Controls[1];
  dropdownOfEmployees.SelectedIndex = 2;
}&lt;/pre&gt;
&lt;h3&gt;Proper Data Binding
&lt;/h3&gt;
&lt;p&gt;
This is the essence of Data Binding within ASP.NET Web Forms: one method and three
events. When done properly, the DataBind method should only be called once on a control
tree. Execute DataBind directly on the page, or execute it once on individual controls
such as each of your top-level Repeaters, but whatever path you follow, make sure
that the method is not executed twice on any single control. Use the binding events
to make this happen. Failure to do so can cause unfortunate side effects and extensive
pain.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
DataSources should be assigned prior to execution of a control's DataBind method. 
&lt;/li&gt;
&lt;li&gt;
Controls without a DataSource property can be manually bound using the controls DataBinding
event, after executing DataBind. 
&lt;/li&gt;
&lt;li&gt;
For collection-based container controls, define child controls and assign their DataSource
using the RowCreated/ItemCreated events. 
&lt;/li&gt;
&lt;li&gt;
For collection-based container controls, perform data-dependent logic on child controls
in the RowDataBound/ItemDataBound event. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The ASP.NET Page Life Cycle may seem like a monotonous mess, but it is a useful tool
if you understand the events, their order, and their relationship to each other. If
you missed them, go back and review &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Part
1&lt;/a&gt; for the core Page Life Cycle events and to &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;Part
2&lt;/a&gt; for the Event Execution Order between a page and its controls. Once you have
these down, there still are some oddities to look out for (and some tricks to help
you out). &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]" href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;As
the series continues&lt;/a&gt;, we discuss some of these tips, tricks, and traps to help
you unleash the full power of your ASP.NET applications.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:233f1d3b-6345-4d60-a6cb-b67b55265237" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Data+Binding" rel="tag"&gt;Data Binding&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6682b44d-7660-4b5e-8746-bdcfdae81f84" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=1d5d9618-a51a-471f-a5ec-7381c916f11d</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1d5d9618-a51a-471f-a5ec-7381c916f11d</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On Tuesday, February 9th, I was scheduled lead a jam session for Come Jam With Us,
the software developer study group in Ann Arbor. The session was to be on ASP.NET
MVC 2, aimed to give attendees enough of an introduction to the product to empower
developers to be able to start coding their own ASP.NET MVC 2 projects. Unfortunately,
Mother Nature did not cooperate that day, half of the state of Michigan seemingly
shut down under 8" of snow, and the session was cancelled and rescheduled for
February 23rd. The goal of these Learn to Code exercises is to give you an introduction
to building applications with ASP.NET MVC 2. In the near future, I also hope to provide
a screen cast of these same exercises.
</p>
        <h3>About this Exercise
</h3>
        <p>
This coding exercise is designed to give you an introduction to ASP.NET MVC 2. In
this exercise, developers will create their first database-driven ASP.NET MVC 2 application
within Visual Studio, primarily using code generation built in to Visual Studio. Developers
performing this exercise should be familiar with ASP.NET development and Visual Studio,
but no previous experience with ASP.NET MVC is required.
</p>
        <h3>Prerequisites
</h3>
        <p>
You will need few things for ASP.NET MVC 2 application development and to complete
these exercises. Please complete the following prerequisites prior to moving on. The
session is designed to be completed in about an hour, but prerequisite setup is not
included in that time.
</p>
        <ul>
          <li>
Install <a href="http://www.microsoft.com/visualstudio/">Microsoft Visual Studio 2008
SP1</a> or <a href="http://www.microsoft.com/express/Web/">Visual Web Developer 2008
Express</a> with SP1 
</li>
          <li>
Install <a href="http://www.microsoft.com/sqlserver/2008/">Microsoft SQL Server</a> or <a href="http://www.microsoft.com/express/Database/">SQL
Server Express</a></li>
          <li>
Download and install the latest <a title="ASP.NET MVC 2 RC 2 on Microsoft" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;displaylang=en">ASP.NET
MVC 2 RC 2 bits from Microsoft</a></li>
        </ul>
        <h3>Exercise 0: Getting Started
</h3>
        <h4>Creating a Project
</h4>
        <p>
Before any coding can occur, the first thing that we have to do is create a new ASP.NET
MVC 2 project from within Visual Studio.
</p>
        <ol>
          <li>
In Visual Studio, create a new "ASP.NET MVC 2 Web Application" named MvcJamSession.
You can create your project in either Visual Basic or Visual C#, though all of the
examples in this post will be in C#. 
</li>
          <li>
After selecting the project type and solution/project name, you will be prompted for
creating a unit test project. Select "Yes." Though we will not be using
these tests in this exercise, we will be in future installments. 
</li>
          <li>
Be sure that your MvcJamSession.Test project includes a project reference back to
your MvcJamSession project. 
</li>
          <li>
Compile and run. Your browser should display a blue web site showing "Welcome
to ASP.NET MVC!" Congratulations. You now have your first ASP.NET MVC application. 
</li>
        </ol>
        <h4>Convention-Based Development
</h4>
        <p>
          <a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession.jpg">
            <img style="border-right-width: 0px; margin: 0px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Project Folder Structure of MVC Jam Session Project" border="0" alt="Project Folder Structure of MVC Jam Session Project" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession_thumb.jpg" width="200" height="382" />
          </a> Development
within ASP.NET MVC is based on convention over configuration. Certain naming conventions
are built in the system to eliminate the amount of boiler-plate code that you need
to recreate. That is not to say that you <em>must</em> follow these naming conventions—you
may use whatever convention you like—but by straying from the standardized conventions,
you will be creating a lot of extra work for yourself. With our new ASP.NET MVC 2
Project, a few of these conventions are immediately visible.
</p>
        <ul>
          <li>
            <em>Controllers</em> folder. This is where all Controller classes must go. Individual
classes must be named <em>&lt;Name&gt;Controller</em>. The default project includes <em>HomeController</em>,
which governs the Home and About actions, and <em>AccountController</em>, which governs
the log in, log out, change password, and registration actions. If we were to make
an application that manages Widgets, we would likely have a class named <em>WidgetController</em>. 
</li>
          <li>
            <em>Views</em> folder. This is where all of the Views must go. By default, views are
paired one-to-one with controller actions, such as one view for new user registration
and another view for changing your password. Views are also separated into folders
matching the associated controller name—<em>/Views/&lt;ControllerName&gt;/&lt;ActionName&gt;</em>.
Thus, <em>HomeController</em>'s About action is associated with the <em>/Views/Home/About</em> view. 
<br />
The Views folder also contains a <em>Shared</em> folder. This folder is where any
common views, such as a Master Page, would reside. The <em>Shared</em> folder is also
where the ViewEngine cascades to when it can't find an appropriate view in the /<em>Views/&lt;ControllerName&gt;</em> folder;
if <em>/Views/Home/About</em> didn't exist, the ViewEngine would look for <em>/Views/Shared/About</em>.
This can come in handy for common pages shared by all controllers, such as an error
page. 
</li>
        </ul>
        <h3>Session Exercise 1: Building an Application
</h3>
        <p>
Using the project we just created, we're going to create an application that manage
a list of employees, including their name, job title, date of hire, and date of termination.
Though the default project is a great help on some applications, it can get in the
way on others; we're not going to need any account services in our application, so
we need to first trim the project down a little.
</p>
        <ol>
          <li>
Delete the entire <em>Account</em> folder under /Views/. 
</li>
          <li>
Delete <em>AccountController.cs</em> from the Controllers folder. 
</li>
          <li>
Delete shared partial view <em>/Views/Shared/LogOnUserControl.ascx</em>. 
</li>
          <li>
Delete reference to this partial view by removing the "LoginDisplay" DIV
from<em> /Views/Shared/Site.Master</em>, lines 18-20. 
</li>
          <li>
Removing <em>LoginDisplay</em> will cause a slight layout problem from the CSS. To
fix it, modify the <em>#menucontainer</em> definition in <em>/Content/Site.css</em> on
line 263. 
<br /><pre class="css:nocontrols" name="code">#menucontainer
{
    padding-top:40px;
}</pre></li>
          <li>
Save all, compile, and Run. The site should be functioning normally, but without the
Log In link in the top right of the home page. 
</li>
        </ol>
        <h4>Creating a Database
</h4>
        <p>
Like any dynamic web site, we need a storage mechanism. Create a database in SQL Server
or SQL Server Express with an Employee table containing columns for name, job title,
hired date, and termination date, as well as an identity column for finding records.<a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_1.jpg"><img style="border-right-width: 0px; margin: 5px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MvcJamSession-EmployeeTable" border="0" alt="MvcJamSession-EmployeeTable" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_thumb_1.jpg" width="250" height="283" /></a></p>
        <ul>
          <li>
For those with SQL Server Express, create a new database through right-clicking the
App_Data folder, and adding a new item. The new item should be a SQL Server Database. 
</li>
          <li>
For those with SQL Server, create a new database through SQL Server Management Studio
(SSMS). 
</li>
        </ul>
        <ol>
          <li>
Create a new table called <em>Employee</em>. 
</li>
          <li>
Create the following columns within the new table: 
<ul><li>
Id (primary key, identity, int, not null) 
</li><li>
Name (varchar(50), not null) 
</li><li>
JobTitle (varchar(50), not null) 
</li><li>
HiredOn (date, not null) 
</li><li>
TerminatedOn (date, null) 
</li></ul></li>
          <li>
Add a record or two to the table for good measure. 
</li>
        </ol>
        <h4>Creating a Model
</h4>
        <p>
The next thing we need to create is our Model. Not only is it the code representation
for our business entity (An Employee class contains Name, JobTitle, HiredOn, and TerminatedOn
properties), it is also responsible for <em>how</em> to get, save, or delete data
from the database. We will be using Microsoft Entity Framework through Visual Studio
to generate our model for us.
</p>
        <ol>
          <li>
To create our new Model, right-click the <em>Model</em> folder and select Add New
Item. The new item should be an ADO.NET Entity Data Model, found within the Data category
of the Add New Item dialog. Name it MvcJamSessionEntities. 
</li>
          <li>
Generate the Model from a database, connecting to your MVC Jam Session database created
in the previous section. On the step where you select your database connection, be
sure to allow Visual Studio to add the connection string to your web.config by checking
the appropriate checkbox. 
</li>
          <li>
The <em>Employee</em> table is the only Data Object that needs to be generated. 
</li>
          <li>
When the dialog completes, your Entity Data Model (a .edmx file) should be displayed
in design view. 
</li>
          <li>
Save all and compile. 
</li>
        </ol>
        <h4>Creating a Controller
</h4>
        <p>
Now that our model is in place, we need to create a controller. The controller is
responsible for managing all interaction between the end-user and the application,
including identifying what data to get, save, or delete. (Remember, though the Controller
is responsible for <em>what</em>, the Model is responsible for <em>how</em>.)
</p>
        <ol>
          <li>
To create our new Controller, right-click the <em>Controller</em> folder and select
Add Controller. Since the name matters in ASP.NET MVC's convention-over-configuration
style, name it EmployeeController.cs (or .vb, if you happen to be working in Visual
Basic .NET). Be sure to check the "Add action methods for Create, Update, Detail,
and Delete scenarios" as we will be using them later. 
</li>
          <li>
We now have a basic controller, but it doesn't do anything yet. First, we want to
modify our Index action, as it is the default action in the controller. We will use
this action to list all of the Employees that currently exist in our database. Your
Index() action method should contain the following code: 
<br /><pre class="csharp:nocontrols" name="code">var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.ToList());</pre></li>
          <li>
Save all and compile. 
</li>
        </ol>
        <h4>Creating a View
</h4>
        <p>
We have our Model and we have our Controller, so it is time for our View. The View
is responsible for display only—it should contain virtually no logic. Our controller
doesn't do anything yet, but we can at least get the base file structure and navigation
in place. Since our Model governs <em>how</em> and our Controller governs <em>what</em>,
think of the View as governing <em>where</em>, as it is responsible for deciding where
each data element gets displayed on your page.
</p>
        <ol>
          <li>
Once you have saved and compiled your Controller, right-click on the Index action
name and select Add View. 
</li>
          <li>
The View should be named Index, just like your Controller action. Also, make your
View strongly typed to the Employee model. Since a list of Employees is being passed
to the View from the Controller, making the View strongly-typed prevents us from having
to cast our View Model from object to Employee. Finally, since we are providing a
list of Employees, the View Content should be a List. 
</li>
          <li>
The <em>Employee</em> folder should be automatically created under the <em>/Views/</em> folder,
and the <em>Index.aspx</em> View inside of this new <em>Employee</em> folder. 
</li>
          <li>
The last thing we need to do is provide some navigation to this view. Open up <em>/Views/Shared/Site.Master</em> and
add an ActionLink within the Menu Container section to the Index action of the Employee
controller. When you are done, the Menu Container should look like this: 
<br /><pre class="csharp:nocontrols" name="code">&lt;ul id="menu"&gt;              
  &lt;li&gt;&lt;%= Html.ActionLink("Home", "Index", "Home")%&gt;&lt;/li&gt;
  &lt;li&gt;&lt;%= Html.ActionLink("About", "About", "Home")%&gt;&lt;/li&gt;
  &lt;li&gt;&lt;%= Html.ActionLink("Employees", "Index", "Employee")%&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></li>
          <li>
Save all and run. When you navigate to your Employees link, you should get a list
of all employees currently in the database with an Edit, Details, and Delete links. 
</li>
        </ol>
        <blockquote>
          <h5>New in ASP.NET MVC 2: Strongly Typed HTML Helpers
</h5>
          <p>
In the previous version of ASP.NET MVC, HTML helpers were simple generic classes.
Generated views were full of Magic Strings for each property in your model, such as
&lt;%= Html.TextBox("Name") %&gt;, opening the door for a fat-fingered property
name. MVC 2 includes strongly-typed HTML helpers on strongly-typed views. For form-based
views, use the new strongly-typed "For" methods, such as &lt;%= Html.TextBoxFor(model
=&gt; model.Name) %&gt; or &lt;%= Html.EditorFor(model =&gt; model.Name) %&gt; to
eliminate the risk of incorrectly entering a property name. For display fields, use
Html.DisplayFor() to provide similar benefits for your read-only data, including the
elimination of HTML encoding for each field.
</p>
        </blockquote>
        <h4>Adding New Data
</h4>
        <p>
A list of employees is great, but we also need the ability to manipulate that data.
First, let's start with the ability to create new data.
</p>
        <ol>
          <li>
Within Visual Studio, open <em>/Controllers/EmployeeController</em> and navigate to
the POST Create action. POST is an HTTP verb associated with pushing data to the server
in the HTTP header, commonly associated with form submits. GET is the HTTP verb for
pure retrieval, commonly associated with a direct URL request, such as clicking a
link. In this case, the POST action can be identified by the [HttpPost] decoration. 
</li>
          <li>
The method arguments currently include only a FormCollection object that will contain
all of the header values associated with the POST. However, MVC is smart; it can automatically
transform this collection into a type-safe Model, based on the names of the header
variables (the identities of the HTML form inputs match the names of the Model's properties).
The one exception is the Id attribute, which is available in the Model but not populated
until after the object is saved to the database. To get strong typing, and avoid having
to manually cast or map form collection data to a new instance of our Model, change
the method signature to the following: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Create([Bind(Exclude="Id")] Employee newEmployee)</pre></li>
          <li>
Now that we have a populated Model, we just need to save the Model to the database
and redirect back to the List when we are done. Do this by replacing the contents
of the action method with the following code: 
<br /><pre class="csharp:nocontrols" name="code">try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  _entities.AddToEmployee(employee);
  _entities.SaveChanges();
  return RedirectToAction("Index");
}
catch
{
  return View();
}</pre></li>
          <li>
Save and compile. 
</li>
          <li>
Now we must create the View for adding data. As we did with Index, we can create the
view by right-clicking the Action method and selecting Add View. The view content
should be Create. 
</li>
          <li>
The only modification for the Create view is the Id property. The generated code creates
an input for this property, but it is not needed since the column value is auto-populated
by the database when saving the entity. Remove this input and label from the form. 
</li>
          <li>
Save and run. You should now be able to add new records to the database. 
</li>
        </ol>
        <blockquote>
          <h5>New in ASP.NET MVC2: Better Verb Attributes
</h5>
          <p>
In the first version of ASP.NET MVC, HTTP Verb conditions were placed on an Action
via the AcceptVerbsAttribute, such as the Create action's [AcceptVerbs(HttpVerbs.Post)].
In ASP.NET MVC 2, these attributes have been simplified with the introduction of the
HttpGetAttribute, HttpDeleteAttribute, HttpPostAttribute, and HttpPutAttribute.
</p>
        </blockquote>
        <h4>Routing and Updates
</h4>
        <p>
The end user can view a list of Employees, and can create new employees, but when
the end user clicks the Edit or Detail links, they get an error since these Views
haven't been created yet and the Actions are not implemented. One by one, we will
get the new views in place.
</p>
        <ol>
          <li>
Within Visual Studio, open <em>/Controllers/EmployeeController</em> and navigate to
the Details action. You may notice that the method already accepts an integer as input,
and shows example usage of the action in a commented URL above the method: <em>/Employee/Details/5</em>.
This integer is the identity value of the Employee record, and is already populated
in the links of our List view created in the previous section. 
</li>
          <li>
Within Visual Studio, open <em>Global.asax</em> and navigate to the RegisterRoutes
method. The default route for ASP.NET MVC is <em>/{controller}/{action}/{id}/</em>.
By parsing out any URL into the application, MVC can determine which Controller to
use, which Action to execute, and which arguments to pass to the Action. Later portions
of the URL path are often optional, and when not specified, are replaced with the
default values: Home, Index, and String.Empty. The URLs of "/", "/Home",
"/Home/", "/Home/Index", and "/Home/Index/" are all
equivalent URLs in the eyes of ASP.NET MVC. 
</li>
        </ol>
        <p>
Go back to the Employee controller. Now that we know what the integer argument is
for, we need to retrieve the Employee matching the associated identity and pass it
to a view for editing or display.
</p>
        <ol>
          <li>
Replace the contents of the GET version of the Edit action method with the following
code to retrieve the Employee from the database that matches the identity specified
in the route: 
<br /><pre class="csharp:nocontrols" name="code">var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.Where(emp =&gt; emp.Id == id).First());</pre></li>
          <li>
Save and compile. 
</li>
          <li>
Right-click the Action and add the View. The View should still be strongly typed,
but this time the view content should be Edit. 
</li>
          <li>
The Details and GET Delete actions are largely similar as the GET Edit action, except
that the view is labels instead of text boxes. Repeat the above three steps for the
Details action method with a Details view content and for the GET Delete action method
with a Delete view content. 
</li>
          <li>
As with the Create view, the Id property should be removed from the Edit view, as
it is not an item that should be edited by the end user. 
</li>
          <li>
Save and run. You should see the details of an Employee when clicking on the Edit
and Details links. 
</li>
          <li>
We can view Employee details within the Edit form, but when we make changes and submit,
nothing happens. We need to modify the POST Edit action to save our changes back to
the database. The default POST Edit action accepts an id and a FormCollection as input
arguments, but similarly to the POST Create action, we can change this to use our
strongly typed model to avoid having to cast or map data. However, unlike our Create
action, we need to bind the id property so that the system knows which record to update.
To make these modifications, replace the POST Edit signature with the following: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Edit(MvcJamSession.Models.Employee employee)</pre></li>
          <li>
Replace the contents of the POST Edit action method with the following code to save
the changes to the database: 
<br /><pre class="csharp:nocontrols" name="code">try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  var _originalEmployee =
    _entities.Employee.Where(emp =&gt; emp.Id == employee.Id).First();
  _entities.ApplyPropertyChanges(_originalEmployee.EntityKey.EntitySetName,
                                 employee);
  _entities.SaveChanges();
  return RedirectToAction("Index");
}
catch
{
  return View();
}</pre></li>
          <li>
Replace the signature of the POST Delete action method with the following code to
provide strong typing on our model: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Delete(MvcJamSession.Models.Employee employee)</pre></li>
          <li>
Finally, replace the contents of the POST Delete action method with the following
code to delete a record: 
<br /><pre class="csharp:nocontrols" name="code">var _entites = new MvcJamSession.Models.MvcJamSessionEntities();
var originalEmployee =
  _entites.Employee.Where(emp =&gt; emp.Id == deletedEmployee.Id).First();
_entites.DeleteObject(originalEmployee);
_entites.SaveChanges();
return RedirectToAction("Index");</pre></li>
          <li>
Save, compile, and run. You should now be able to modify existing Employee records. 
</li>
        </ol>
        <p>
We now have a fully-functional ASP.NET MVC application to manage our Employee records.
Congratulations!
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:13d5b303-4a7f-4ac9-93a3-f208ffe24fe0" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag">ASP.NET MVC</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/Jam+Session" rel="tag">Jam
Session</a>,<a href="http://technorati.com/tags/Coding+Exercise" rel="tag">Coding
Exercise</a>,<a href="http://technorati.com/tags/Learn+to+Code" rel="tag">Learn to
Code</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d5d9618-a51a-471f-a5ec-7381c916f11d" />
      </body>
      <title>Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</guid>
      <link>http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx</link>
      <pubDate>Tue, 23 Feb 2010 22:38:00 GMT</pubDate>
      <description>&lt;p&gt;
On Tuesday, February 9th, I was scheduled lead a jam session for Come Jam With Us,
the software developer study group in Ann Arbor. The session was to be on ASP.NET
MVC 2, aimed to give attendees enough of an introduction to the product to empower
developers to be able to start coding their own ASP.NET MVC 2 projects. Unfortunately,
Mother Nature did not cooperate that day, half of the state of Michigan seemingly
shut down under 8&amp;quot; of snow, and the session was cancelled and rescheduled for
February 23rd. The goal of these Learn to Code exercises is to give you an introduction
to building applications with ASP.NET MVC 2. In the near future, I also hope to provide
a screen cast of these same exercises.
&lt;/p&gt;
&lt;h3&gt;About this Exercise
&lt;/h3&gt;
&lt;p&gt;
This coding exercise is designed to give you an introduction to ASP.NET MVC 2. In
this exercise, developers will create their first database-driven ASP.NET MVC 2 application
within Visual Studio, primarily using code generation built in to Visual Studio. Developers
performing this exercise should be familiar with ASP.NET development and Visual Studio,
but no previous experience with ASP.NET MVC is required.
&lt;/p&gt;
&lt;h3&gt;Prerequisites
&lt;/h3&gt;
&lt;p&gt;
You will need few things for ASP.NET MVC 2 application development and to complete
these exercises. Please complete the following prerequisites prior to moving on. The
session is designed to be completed in about an hour, but prerequisite setup is not
included in that time.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/visualstudio/"&gt;Microsoft Visual Studio 2008
SP1&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Web/"&gt;Visual Web Developer 2008
Express&lt;/a&gt; with SP1 
&lt;/li&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/sqlserver/2008/"&gt;Microsoft SQL Server&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Database/"&gt;SQL
Server Express&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Download and install the latest &lt;a title="ASP.NET MVC 2 RC 2 on Microsoft" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;amp;displaylang=en"&gt;ASP.NET
MVC 2 RC 2 bits from Microsoft&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Exercise 0: Getting Started
&lt;/h3&gt;
&lt;h4&gt;Creating a Project
&lt;/h4&gt;
&lt;p&gt;
Before any coding can occur, the first thing that we have to do is create a new ASP.NET
MVC 2 project from within Visual Studio.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
In Visual Studio, create a new &amp;quot;ASP.NET MVC 2 Web Application&amp;quot; named MvcJamSession.
You can create your project in either Visual Basic or Visual C#, though all of the
examples in this post will be in C#. 
&lt;/li&gt;
&lt;li&gt;
After selecting the project type and solution/project name, you will be prompted for
creating a unit test project. Select &amp;quot;Yes.&amp;quot; Though we will not be using
these tests in this exercise, we will be in future installments. 
&lt;/li&gt;
&lt;li&gt;
Be sure that your MvcJamSession.Test project includes a project reference back to
your MvcJamSession project. 
&lt;/li&gt;
&lt;li&gt;
Compile and run. Your browser should display a blue web site showing &amp;quot;Welcome
to ASP.NET MVC!&amp;quot; Congratulations. You now have your first ASP.NET MVC application. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Convention-Based Development
&lt;/h4&gt;
&lt;p&gt;
&lt;a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Project Folder Structure of MVC Jam Session Project" border="0" alt="Project Folder Structure of MVC Jam Session Project" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession_thumb.jpg" width="200" height="382" /&gt;&lt;/a&gt; Development
within ASP.NET MVC is based on convention over configuration. Certain naming conventions
are built in the system to eliminate the amount of boiler-plate code that you need
to recreate. That is not to say that you &lt;em&gt;must&lt;/em&gt; follow these naming conventions—you
may use whatever convention you like—but by straying from the standardized conventions,
you will be creating a lot of extra work for yourself. With our new ASP.NET MVC 2
Project, a few of these conventions are immediately visible.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Controllers&lt;/em&gt; folder. This is where all Controller classes must go. Individual
classes must be named &lt;em&gt;&amp;lt;Name&amp;gt;Controller&lt;/em&gt;. The default project includes &lt;em&gt;HomeController&lt;/em&gt;,
which governs the Home and About actions, and &lt;em&gt;AccountController&lt;/em&gt;, which governs
the log in, log out, change password, and registration actions. If we were to make
an application that manages Widgets, we would likely have a class named &lt;em&gt;WidgetController&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Views&lt;/em&gt; folder. This is where all of the Views must go. By default, views are
paired one-to-one with controller actions, such as one view for new user registration
and another view for changing your password. Views are also separated into folders
matching the associated controller name—&lt;em&gt;/Views/&amp;lt;ControllerName&amp;gt;/&amp;lt;ActionName&amp;gt;&lt;/em&gt;.
Thus, &lt;em&gt;HomeController&lt;/em&gt;'s About action is associated with the &lt;em&gt;/Views/Home/About&lt;/em&gt; view. 
&lt;br /&gt;
The Views folder also contains a &lt;em&gt;Shared&lt;/em&gt; folder. This folder is where any
common views, such as a Master Page, would reside. The &lt;em&gt;Shared&lt;/em&gt; folder is also
where the ViewEngine cascades to when it can't find an appropriate view in the /&lt;em&gt;Views/&amp;lt;ControllerName&amp;gt;&lt;/em&gt; folder;
if &lt;em&gt;/Views/Home/About&lt;/em&gt; didn't exist, the ViewEngine would look for &lt;em&gt;/Views/Shared/About&lt;/em&gt;.
This can come in handy for common pages shared by all controllers, such as an error
page. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Session Exercise 1: Building an Application
&lt;/h3&gt;
&lt;p&gt;
Using the project we just created, we're going to create an application that manage
a list of employees, including their name, job title, date of hire, and date of termination.
Though the default project is a great help on some applications, it can get in the
way on others; we're not going to need any account services in our application, so
we need to first trim the project down a little.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Delete the entire &lt;em&gt;Account&lt;/em&gt; folder under /Views/. 
&lt;/li&gt;
&lt;li&gt;
Delete &lt;em&gt;AccountController.cs&lt;/em&gt; from the Controllers folder. 
&lt;/li&gt;
&lt;li&gt;
Delete shared partial view &lt;em&gt;/Views/Shared/LogOnUserControl.ascx&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
Delete reference to this partial view by removing the &amp;quot;LoginDisplay&amp;quot; DIV
from&lt;em&gt; /Views/Shared/Site.Master&lt;/em&gt;, lines 18-20. 
&lt;/li&gt;
&lt;li&gt;
Removing &lt;em&gt;LoginDisplay&lt;/em&gt; will cause a slight layout problem from the CSS. To
fix it, modify the &lt;em&gt;#menucontainer&lt;/em&gt; definition in &lt;em&gt;/Content/Site.css&lt;/em&gt; on
line 263. 
&lt;br /&gt;
&lt;pre class="css:nocontrols" name="code"&gt;#menucontainer
{
    padding-top:40px;
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all, compile, and Run. The site should be functioning normally, but without the
Log In link in the top right of the home page. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Database
&lt;/h4&gt;
&lt;p&gt;
Like any dynamic web site, we need a storage mechanism. Create a database in SQL Server
or SQL Server Express with an Employee table containing columns for name, job title,
hired date, and termination date, as well as an identity column for finding records.&lt;a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_1.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MvcJamSession-EmployeeTable" border="0" alt="MvcJamSession-EmployeeTable" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_thumb_1.jpg" width="250" height="283" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
For those with SQL Server Express, create a new database through right-clicking the
App_Data folder, and adding a new item. The new item should be a SQL Server Database. 
&lt;/li&gt;
&lt;li&gt;
For those with SQL Server, create a new database through SQL Server Management Studio
(SSMS). 
&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new table called &lt;em&gt;Employee&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
Create the following columns within the new table: 
&lt;ul&gt;
&lt;li&gt;
Id (primary key, identity, int, not null) 
&lt;/li&gt;
&lt;li&gt;
Name (varchar(50), not null) 
&lt;/li&gt;
&lt;li&gt;
JobTitle (varchar(50), not null) 
&lt;/li&gt;
&lt;li&gt;
HiredOn (date, not null) 
&lt;/li&gt;
&lt;li&gt;
TerminatedOn (date, null) 
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Add a record or two to the table for good measure. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Model
&lt;/h4&gt;
&lt;p&gt;
The next thing we need to create is our Model. Not only is it the code representation
for our business entity (An Employee class contains Name, JobTitle, HiredOn, and TerminatedOn
properties), it is also responsible for &lt;em&gt;how&lt;/em&gt; to get, save, or delete data
from the database. We will be using Microsoft Entity Framework through Visual Studio
to generate our model for us.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
To create our new Model, right-click the &lt;em&gt;Model&lt;/em&gt; folder and select Add New
Item. The new item should be an ADO.NET Entity Data Model, found within the Data category
of the Add New Item dialog. Name it MvcJamSessionEntities. 
&lt;/li&gt;
&lt;li&gt;
Generate the Model from a database, connecting to your MVC Jam Session database created
in the previous section. On the step where you select your database connection, be
sure to allow Visual Studio to add the connection string to your web.config by checking
the appropriate checkbox. 
&lt;/li&gt;
&lt;li&gt;
The &lt;em&gt;Employee&lt;/em&gt; table is the only Data Object that needs to be generated. 
&lt;/li&gt;
&lt;li&gt;
When the dialog completes, your Entity Data Model (a .edmx file) should be displayed
in design view. 
&lt;/li&gt;
&lt;li&gt;
Save all and compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Controller
&lt;/h4&gt;
&lt;p&gt;
Now that our model is in place, we need to create a controller. The controller is
responsible for managing all interaction between the end-user and the application,
including identifying what data to get, save, or delete. (Remember, though the Controller
is responsible for &lt;em&gt;what&lt;/em&gt;, the Model is responsible for &lt;em&gt;how&lt;/em&gt;.)
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
To create our new Controller, right-click the &lt;em&gt;Controller&lt;/em&gt; folder and select
Add Controller. Since the name matters in ASP.NET MVC's convention-over-configuration
style, name it EmployeeController.cs (or .vb, if you happen to be working in Visual
Basic .NET). Be sure to check the &amp;quot;Add action methods for Create, Update, Detail,
and Delete scenarios&amp;quot; as we will be using them later. 
&lt;/li&gt;
&lt;li&gt;
We now have a basic controller, but it doesn't do anything yet. First, we want to
modify our Index action, as it is the default action in the controller. We will use
this action to list all of the Employees that currently exist in our database. Your
Index() action method should contain the following code: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.ToList());&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all and compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a View
&lt;/h4&gt;
&lt;p&gt;
We have our Model and we have our Controller, so it is time for our View. The View
is responsible for display only—it should contain virtually no logic. Our controller
doesn't do anything yet, but we can at least get the base file structure and navigation
in place. Since our Model governs &lt;em&gt;how&lt;/em&gt; and our Controller governs &lt;em&gt;what&lt;/em&gt;,
think of the View as governing &lt;em&gt;where&lt;/em&gt;, as it is responsible for deciding where
each data element gets displayed on your page.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Once you have saved and compiled your Controller, right-click on the Index action
name and select Add View. 
&lt;/li&gt;
&lt;li&gt;
The View should be named Index, just like your Controller action. Also, make your
View strongly typed to the Employee model. Since a list of Employees is being passed
to the View from the Controller, making the View strongly-typed prevents us from having
to cast our View Model from object to Employee. Finally, since we are providing a
list of Employees, the View Content should be a List. 
&lt;/li&gt;
&lt;li&gt;
The &lt;em&gt;Employee&lt;/em&gt; folder should be automatically created under the &lt;em&gt;/Views/&lt;/em&gt; folder,
and the &lt;em&gt;Index.aspx&lt;/em&gt; View inside of this new &lt;em&gt;Employee&lt;/em&gt; folder. 
&lt;/li&gt;
&lt;li&gt;
The last thing we need to do is provide some navigation to this view. Open up &lt;em&gt;/Views/Shared/Site.Master&lt;/em&gt; and
add an ActionLink within the Menu Container section to the Index action of the Employee
controller. When you are done, the Menu Container should look like this: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;&amp;lt;ul id=&amp;quot;menu&amp;quot;&amp;gt;              
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;Home&amp;quot;, &amp;quot;Index&amp;quot;, &amp;quot;Home&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;About&amp;quot;, &amp;quot;About&amp;quot;, &amp;quot;Home&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;Employees&amp;quot;, &amp;quot;Index&amp;quot;, &amp;quot;Employee&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all and run. When you navigate to your Employees link, you should get a list
of all employees currently in the database with an Edit, Details, and Delete links. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt; 
&lt;h5&gt;New in ASP.NET MVC 2: Strongly Typed HTML Helpers
&lt;/h5&gt;
&lt;p&gt;
In the previous version of ASP.NET MVC, HTML helpers were simple generic classes.
Generated views were full of Magic Strings for each property in your model, such as
&amp;lt;%= Html.TextBox(&amp;quot;Name&amp;quot;) %&amp;gt;, opening the door for a fat-fingered property
name. MVC 2 includes strongly-typed HTML helpers on strongly-typed views. For form-based
views, use the new strongly-typed &amp;quot;For&amp;quot; methods, such as &amp;lt;%= Html.TextBoxFor(model
=&amp;gt; model.Name) %&amp;gt; or &amp;lt;%= Html.EditorFor(model =&amp;gt; model.Name) %&amp;gt; to
eliminate the risk of incorrectly entering a property name. For display fields, use
Html.DisplayFor() to provide similar benefits for your read-only data, including the
elimination of HTML encoding for each field.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h4&gt;Adding New Data
&lt;/h4&gt;
&lt;p&gt;
A list of employees is great, but we also need the ability to manipulate that data.
First, let's start with the ability to create new data.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;/Controllers/EmployeeController&lt;/em&gt; and navigate to
the POST Create action. POST is an HTTP verb associated with pushing data to the server
in the HTTP header, commonly associated with form submits. GET is the HTTP verb for
pure retrieval, commonly associated with a direct URL request, such as clicking a
link. In this case, the POST action can be identified by the [HttpPost] decoration. 
&lt;/li&gt;
&lt;li&gt;
The method arguments currently include only a FormCollection object that will contain
all of the header values associated with the POST. However, MVC is smart; it can automatically
transform this collection into a type-safe Model, based on the names of the header
variables (the identities of the HTML form inputs match the names of the Model's properties).
The one exception is the Id attribute, which is available in the Model but not populated
until after the object is saved to the database. To get strong typing, and avoid having
to manually cast or map form collection data to a new instance of our Model, change
the method signature to the following: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Create([Bind(Exclude=&amp;quot;Id&amp;quot;)] Employee newEmployee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Now that we have a populated Model, we just need to save the Model to the database
and redirect back to the List when we are done. Do this by replacing the contents
of the action method with the following code: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  _entities.AddToEmployee(employee);
  _entities.SaveChanges();
  return RedirectToAction(&amp;quot;Index&amp;quot;);
}
catch
{
  return View();
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save and compile. 
&lt;/li&gt;
&lt;li&gt;
Now we must create the View for adding data. As we did with Index, we can create the
view by right-clicking the Action method and selecting Add View. The view content
should be Create. 
&lt;/li&gt;
&lt;li&gt;
The only modification for the Create view is the Id property. The generated code creates
an input for this property, but it is not needed since the column value is auto-populated
by the database when saving the entity. Remove this input and label from the form. 
&lt;/li&gt;
&lt;li&gt;
Save and run. You should now be able to add new records to the database. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt; 
&lt;h5&gt;New in ASP.NET MVC2: Better Verb Attributes
&lt;/h5&gt;
&lt;p&gt;
In the first version of ASP.NET MVC, HTTP Verb conditions were placed on an Action
via the AcceptVerbsAttribute, such as the Create action's [AcceptVerbs(HttpVerbs.Post)].
In ASP.NET MVC 2, these attributes have been simplified with the introduction of the
HttpGetAttribute, HttpDeleteAttribute, HttpPostAttribute, and HttpPutAttribute.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h4&gt;Routing and Updates
&lt;/h4&gt;
&lt;p&gt;
The end user can view a list of Employees, and can create new employees, but when
the end user clicks the Edit or Detail links, they get an error since these Views
haven't been created yet and the Actions are not implemented. One by one, we will
get the new views in place.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;/Controllers/EmployeeController&lt;/em&gt; and navigate to
the Details action. You may notice that the method already accepts an integer as input,
and shows example usage of the action in a commented URL above the method: &lt;em&gt;/Employee/Details/5&lt;/em&gt;.
This integer is the identity value of the Employee record, and is already populated
in the links of our List view created in the previous section. 
&lt;/li&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;Global.asax&lt;/em&gt; and navigate to the RegisterRoutes
method. The default route for ASP.NET MVC is &lt;em&gt;/{controller}/{action}/{id}/&lt;/em&gt;.
By parsing out any URL into the application, MVC can determine which Controller to
use, which Action to execute, and which arguments to pass to the Action. Later portions
of the URL path are often optional, and when not specified, are replaced with the
default values: Home, Index, and String.Empty. The URLs of &amp;quot;/&amp;quot;, &amp;quot;/Home&amp;quot;,
&amp;quot;/Home/&amp;quot;, &amp;quot;/Home/Index&amp;quot;, and &amp;quot;/Home/Index/&amp;quot; are all
equivalent URLs in the eyes of ASP.NET MVC. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Go back to the Employee controller. Now that we know what the integer argument is
for, we need to retrieve the Employee matching the associated identity and pass it
to a view for editing or display.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Replace the contents of the GET version of the Edit action method with the following
code to retrieve the Employee from the database that matches the identity specified
in the route: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.Where(emp =&amp;gt; emp.Id == id).First());&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save and compile. 
&lt;/li&gt;
&lt;li&gt;
Right-click the Action and add the View. The View should still be strongly typed,
but this time the view content should be Edit. 
&lt;/li&gt;
&lt;li&gt;
The Details and GET Delete actions are largely similar as the GET Edit action, except
that the view is labels instead of text boxes. Repeat the above three steps for the
Details action method with a Details view content and for the GET Delete action method
with a Delete view content. 
&lt;/li&gt;
&lt;li&gt;
As with the Create view, the Id property should be removed from the Edit view, as
it is not an item that should be edited by the end user. 
&lt;/li&gt;
&lt;li&gt;
Save and run. You should see the details of an Employee when clicking on the Edit
and Details links. 
&lt;/li&gt;
&lt;li&gt;
We can view Employee details within the Edit form, but when we make changes and submit,
nothing happens. We need to modify the POST Edit action to save our changes back to
the database. The default POST Edit action accepts an id and a FormCollection as input
arguments, but similarly to the POST Create action, we can change this to use our
strongly typed model to avoid having to cast or map data. However, unlike our Create
action, we need to bind the id property so that the system knows which record to update.
To make these modifications, replace the POST Edit signature with the following: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Edit(MvcJamSession.Models.Employee employee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Replace the contents of the POST Edit action method with the following code to save
the changes to the database: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  var _originalEmployee =
    _entities.Employee.Where(emp =&amp;gt; emp.Id == employee.Id).First();
  _entities.ApplyPropertyChanges(_originalEmployee.EntityKey.EntitySetName,
                                 employee);
  _entities.SaveChanges();
  return RedirectToAction(&amp;quot;Index&amp;quot;);
}
catch
{
  return View();
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Replace the signature of the POST Delete action method with the following code to
provide strong typing on our model: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Delete(MvcJamSession.Models.Employee employee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Finally, replace the contents of the POST Delete action method with the following
code to delete a record: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entites = new MvcJamSession.Models.MvcJamSessionEntities();
var originalEmployee =
  _entites.Employee.Where(emp =&amp;gt; emp.Id == deletedEmployee.Id).First();
_entites.DeleteObject(originalEmployee);
_entites.SaveChanges();
return RedirectToAction(&amp;quot;Index&amp;quot;);&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save, compile, and run. You should now be able to modify existing Employee records. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
We now have a fully-functional ASP.NET MVC application to manage our Employee records.
Congratulations!
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:13d5b303-4a7f-4ac9-93a3-f208ffe24fe0" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag"&gt;ASP.NET MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Jam+Session" rel="tag"&gt;Jam
Session&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Coding+Exercise" rel="tag"&gt;Coding
Exercise&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Learn+to+Code" rel="tag"&gt;Learn to
Code&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d5d9618-a51a-471f-a5ec-7381c916f11d" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</comments>
      <category>ASP.Net</category>
      <category>Learn to Code</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=f8562c46-4083-4d56-bc7c-42bff947af62</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f8562c46-4083-4d56-bc7c-42bff947af62</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On Tuesday, February 23rd, I will be leading the jam session for <a href="http://www.comejamwithus.org">Come
Jam With Us</a>, the software developer study group in Ann Arbor. The session will
be on ASP.NET MVC, and aims to give attendees enough of an introduction to the product
to empower developers to be able to start coding their own ASP.NET MVC 2 projects.
Like all of the Come Jam With Us sessions for the winter/spring of 2010, it will be
held at the offices of SRT Solutions in Ann Arbor at 5:30p.
</p>
        <h3>Prerequisites
</h3>
        <p>
You will need few things for ASP.NET MVC 2 application development and to complete
this exercise. Please complete the following prerequisites prior to the session, otherwise
you likely will spend the hour downloading and installing rather than coding.
</p>
        <ul>
          <li>
Install <a href="http://www.microsoft.com/visualstudio/">Microsoft Visual Studio 2008
SP1</a> or <a href="http://www.microsoft.com/express/Web/">Visual Web Developer 2008
Express</a> with SP1 
</li>
          <li>
Install <a href="http://www.microsoft.com/sqlserver/2008/">Microsoft SQL Server</a> or <a href="http://www.microsoft.com/express/Database/">SQL
Server Express</a></li>
          <li>
Download and install <a title="ASP.NET MVC 2 on Microsoft.com" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;displaylang=en">ASP.NET
MVC 2 from Microsoft.com</a></li>
        </ul>
        <h3>Rescheduled
</h3>
        <p>
Due to weather, the original February 9th meeting was cancelled. This session is rescheduled
for Tuesday, February 23rd.
</p>
        <p>
        </p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3e0d3cc-b34f-4985-84a1-155c955740ed" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Ann+Arbor" rel="tag">Ann Arbor</a>,<a href="http://technorati.com/tags/Software" rel="tag">Software</a>,<a href="http://technorati.com/tags/Code+Jam" rel="tag">Code
Jam</a>,<a href="http://technorati.com/tags/Study+Group" rel="tag">Study Group</a>,<a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag">ASP.NET
MVC</a>,<a href="http://technorati.com/tags/ASP.NET+MVC2" rel="tag">ASP.NET MVC2</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=f8562c46-4083-4d56-bc7c-42bff947af62" />
      </body>
      <title>Upcoming ASP.NET MVC 2 Study Group Jam Session in Ann Arbor</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</guid>
      <link>http://www.cptloadtest.com/2010/02/08/Upcoming-ASPNET-MVC-2-Study-Group-Jam-Session-In-Ann-Arbor.aspx</link>
      <pubDate>Mon, 08 Feb 2010 04:45:36 GMT</pubDate>
      <description>&lt;p&gt;
On Tuesday, February 23rd, I will be leading the jam session for &lt;a href="http://www.comejamwithus.org"&gt;Come
Jam With Us&lt;/a&gt;, the software developer study group in Ann Arbor. The session will
be on ASP.NET MVC, and aims to give attendees enough of an introduction to the product
to empower developers to be able to start coding their own ASP.NET MVC 2 projects.
Like all of the Come Jam With Us sessions for the winter/spring of 2010, it will be
held at the offices of SRT Solutions in Ann Arbor at 5:30p.
&lt;/p&gt;
&lt;h3&gt;Prerequisites
&lt;/h3&gt;
&lt;p&gt;
You will need few things for ASP.NET MVC 2 application development and to complete
this exercise. Please complete the following prerequisites prior to the session, otherwise
you likely will spend the hour downloading and installing rather than coding.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/visualstudio/"&gt;Microsoft Visual Studio 2008
SP1&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Web/"&gt;Visual Web Developer 2008
Express&lt;/a&gt; with SP1 
&lt;/li&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/sqlserver/2008/"&gt;Microsoft SQL Server&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Database/"&gt;SQL
Server Express&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Download and install &lt;a title="ASP.NET MVC 2 on Microsoft.com" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;amp;displaylang=en"&gt;ASP.NET
MVC 2 from Microsoft.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Rescheduled
&lt;/h3&gt;
&lt;p&gt;
Due to weather, the original February 9th meeting was cancelled. This session is rescheduled
for Tuesday, February 23rd.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3e0d3cc-b34f-4985-84a1-155c955740ed" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Ann+Arbor" rel="tag"&gt;Ann Arbor&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Software" rel="tag"&gt;Software&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Code+Jam" rel="tag"&gt;Code
Jam&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Study+Group" rel="tag"&gt;Study Group&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag"&gt;ASP.NET
MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET+MVC2" rel="tag"&gt;ASP.NET MVC2&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=f8562c46-4083-4d56-bc7c-42bff947af62" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>MVC</category>
    </item>
  </channel>
</rss>