<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schotime.net &#187; .NET</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Dot Less Css With Asp.net MVC 2</title>
		<link>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/</link>
		<comments>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 14:33:27 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[dotLess]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/</guid>
		<description><![CDATA[I have been having a look at the best way to theme a asp.net mvc website in the last few weeks. I heard about dot less css late last year but hadn’t had time to integrate it into a project until now. 
Integrating dot less css is pretty easy by default, as shown on its [...]]]></description>
			<content:encoded><![CDATA[<p>I have been having a look at the best way to theme a asp.net mvc website in the last few weeks. I heard about <a href="http://dotlesscss.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/dotlesscss.com?referer=');">dot less css</a> late last year but hadn’t had time to integrate it into a project until now. </p>
<p>Integrating dot less css is pretty easy by default, as shown on its home page however I wanted something that could be configured by application users. We want to be able to specify the link html tag like the following.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400"> 
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">link </span><span style="color: red">href</span><span style="color: blue">=&quot;/public/css/sites.less&quot; </span><span style="color: red">rel</span><span style="color: blue">=&quot;stylesheet&quot; </span><span style="color: red">type</span><span style="color: blue">=&quot;text/css&quot; /&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
<p>There are few steps involved in the solution I have come up with so bare with me as I go through them. Firstly we’ll start with the web.config:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        &lt;</span><span style="background: black; color: #cc7832">compilation</span><span style="background: black; color: white">&gt;
            &lt;</span><span style="background: black; color: #cc7832">buildProviders</span><span style="background: black; color: white">&gt;
                &lt;</span><span style="background: black; color: #cc7832">add </span><span style="background: black; color: white">extension=&quot;</span><span style="background: black; color: #a5c25c">.lessx</span><span style="background: black; color: white">&quot; type=&quot;</span><span style="background: black; color: #a5c25c">System.Web.Compilation.PageBuildProvider</span><span style="background: black; color: white">&quot; /&gt;
            &lt;/</span><span style="background: black; color: #cc7832">buildProviders</span><span style="background: black; color: white">&gt;
        &lt;/</span><span style="background: black; color: #cc7832">compilation</span><span style="background: black; color: white">&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Insert the builderProviders block inside the compilation section. The compilation section will be under the system.web section. This will enable us to use code blocks in our lessx files.</p>
<p>Secondly, is the routing.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: gray">//Special Route for css so that images are relative to it
        </span><span style="background: black; color: white">routes.MapRoute(</span><span style="background: black; color: #a5c25c">&quot;css&quot;</span><span style="background: black; color: white">,
                           </span><span style="background: black; color: #a5c25c">&quot;public/css/{filename}.less&quot;</span><span style="background: black; color: white">,
                           </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Css&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index” </span><span style="background: black; color: white">},
                           </span><span style="background: black; color: #cc7832">new</span><span style="background: black; color: white">[] { </span><span style="background: black; color: #a5c25c">&quot;eLearning.Controllers&quot; </span><span style="background: black; color: white">});</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This specifies that any url matching /public/css/{filename}.less should be routed to the CssController passing the filename as the parameter to the Index method.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">CssController </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">BaseController
    </span><span style="background: black; color: white">{
        [</span><span style="background: black; color: #ffc66d">OutputCache</span><span style="background: black; color: white">(Duration = </span><span style="background: black; color: #6897bb">10</span><span style="background: black; color: white">, VaryByParam = </span><span style="background: black; color: #a5c25c">&quot;&quot;</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">filename)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">.IsNullOrEmpty(filename))
                </span><span style="background: black; color: #cc7832">throw new </span><span style="background: black; color: #ffc66d">ArgumentException</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;A filename must be supplied&quot;</span><span style="background: black; color: white">);

            ViewData[</span><span style="background: black; color: #a5c25c">&quot;baseColor&quot;</span><span style="background: black; color: white">] = </span><span style="background: black; color: #a5c25c">&quot;#aabbcc&quot;</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">less = RenderViewToString(filename + </span><span style="background: black; color: #a5c25c">&quot;.lessx&quot;</span><span style="background: black; color: white">, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">css = </span><span style="background: black; color: #ffc66d">LessCss</span><span style="background: black; color: white">.Generate(less, </span><span style="background: black; color: #cc7832">true</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">Content(css, </span><span style="background: black; color: #a5c25c">&quot;text/css&quot;</span><span style="background: black; color: white">);
        }
    }

    </span><span style="background: black; color: #cc7832">public static class </span><span style="background: black; color: #ffc66d">LessCss
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public static string </span><span style="background: black; color: white">Generate(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">less, </span><span style="background: black; color: #cc7832">bool </span><span style="background: black; color: white">minify)
        {
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">lessEngine = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">EngineFactory();
            lessEngine.Configuration.MinifyOutput = minify;
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">output = lessEngine.GetEngine().TransformToCss(less, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">output;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is the css controller class. It receives the filename as the input, finds the corresponding .lessx file and returns the file with appropriate view data replaced. Is then takes the less formatted string and runs it through the dot less parser to return a css string. This is then returned to the browser with the appropriate content-type. One method I have not shown is the RenderViewToString() and how the .lessx file is located. This method is located on the base controller class that the css controller inherits from. I have also turned Caching on so that it will cache the result for 10seconds.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #cc7832">protected string </span><span style="background: black; color: white">RenderViewToString(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">viewName, </span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">model)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString(</span><span style="background: black; color: #a5c25c">&quot;action&quot;</span><span style="background: black; color: white">);

            ViewData.Model = model;

            </span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">StringWriter </span><span style="background: black; color: white">sw = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">StringWriter</span><span style="background: black; color: white">())
            {
                </span><span style="background: black; color: #ffc66d">ViewEngineResult </span><span style="background: black; color: white">viewResult = </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.FindView(ControllerContext, viewName, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
                </span><span style="background: black; color: #ffc66d">ViewContext </span><span style="background: black; color: white">viewContext = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">ViewContext</span><span style="background: black; color: white">(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">sw.GetStringBuilder().ToString();
            }
        }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>
  <br />This method finds the .lessx file and returns the result as a string. Now usually the view engine will take controller name and method name and by convention look in the /Views/css/index.aspx file. It doesn’t quite make sense to put it in the views folder so I have overridden the default web forms view engine so that they can be placed in the /Public/Css folder. This can be configured to your liking (convention).</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">CustomViewEngine </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">WebFormViewEngine
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">CustomViewEngine()
        {
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">locs = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">&gt;(</span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ViewLocationFormats);
            locs.Add(</span><span style="background: black; color: #a5c25c">&quot;~/Public/{1}/{0}&quot;</span><span style="background: black; color: white">); </span><span style="background: black; color: gray">//My personal choice
            </span><span style="background: black; color: white">locs.Add(</span><span style="background: black; color: #a5c25c">&quot;~/Views/{1}/{0}&quot;</span><span style="background: black; color: white">);  </span><span style="background: black; color: gray">//An alternative choice
            </span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ViewLocationFormats = locs.ToArray();
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Once your Custom View Engine has been overridden, you will need to add it to the ViewEngines.Engines collection in the application start method in the global.asax.cs.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.Clear();
        </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.Add(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">CustomViewEngine</span><span style="background: black; color: white">());</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This will then allow us to write a *.lessx file like the following; you can also pass a strongly typed model to the .lessx file if you so wish in the same way you would to a regular aspx view.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code">&lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewPage&quot; %&gt;

@basecolor: &lt;%= ViewData[&quot;baseColor&quot;] %&gt;;

body {
    background: @basecolor;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>Which will be rendered to the browser as:</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code">body {
    background: #aabbcc;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>And that’s it. I’m still working on the finer details but I am liking how it works at the moment. If you have any thoughts or suggestions, please leave a comment.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Fluent Validation Model Binder &#8211; Asp.net MVC</title>
		<link>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</link>
		<comments>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:59:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent Validation]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</guid>
		<description><![CDATA[A few weeks ago I found the Fluent Validation framework by Jeremy Skinner. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided.
Since then I [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I found the <a href="http://fluentvalidation.codeplex.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/fluentvalidation.codeplex.com/?referer=');">Fluent Validation</a> framework by <a href="http://www.jeremyskinner.co.uk/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.jeremyskinner.co.uk/?referer=');">Jeremy Skinner</a>. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided.</p>
<p>Since then I have submitted a few patches for the framework, one of which is the Fluent Validation Model binder. Inspired by the Data Annotations Model binder, it works in much the same way. Once you have it set to your default model binder, it will validate any model which contains the specific attribute. This will become clear in the examples below.</p>
<p>Firstly lets take our model.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?"><span style="background: black; color: white">           <br /> 
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">Validator</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">LineItemValidator</span><span style="background: black; color: white">))]
    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItem
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">LineNumber { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #6897bb">DateTime </span><span style="background: black; color: white">Date { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Description { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Net { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Tax { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Gross { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
<p>          <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></p>
<p></span></td>
</tr>
</tbody>
</table>
<p>Attached to this simple LineItem class is a Validator attribute. This attribute is used by the Model Binder to locate the Class used for validation. Below I will define my LineItemValidator class which will hold the rules for the validation.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItemValidator </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AbstractValidator</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt;
    {
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">LineItemValidator()
        {
            RuleFor(x =&gt; x.Description)
                .NotEmpty().When(x =&gt; </span><span style="background: black; color: #ffc66d">Settings</span><span style="background: black; color: white">.DescriptionRequired)
                .And
                .Length(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">30</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Date)
                .GreaterThanOrEqualTo(</span><span style="background: black; color: #6897bb">DateTime</span><span style="background: black; color: white">.Today);

            RuleFor(x =&gt; x.Net)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Gross)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">)
                .And
                .Equal(y =&gt; (y.Net + y.Tax))
                .WithName(</span><span style="background: black; color: #a5c25c">&quot;Total Amount&quot;</span><span style="background: black; color: white">);
        }
    }</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>As you can see the class must inherit from AbstractValidator&lt;T&gt; where T is the model you want to define the rules for. The first rule uses the conditional When clause where it will only validate that the field is NotEmpty when the Settings.DescriptionRequired boolean is true. Also, another thing is the complex validation taking place on the Gross field. Not only does it validate that it is greater than 0, but that the value is equal to the net + tax amount. This is very elegant indeed. I have also specified the ‘WithName’ clause which has also been integrated into the model binder so that if an error occurs with the gross field, the WithName value will be displayed when an error happens. This is extremely handy for language localization or when the name of the field on the Model is insufficient. </p>
<p>Wiring this Model binder up in the Application_start event in the global.asax.cs is as easy as this.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">     </span><span style="background: black; color: #ffc66d">ModelBinders</span><span style="background: black; color: white">.Binders.DefaultBinder =
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">FluentValidationModelBinder</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">AttributedValidatorFactory</span><span style="background: black; color: white">());</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Note that we have to pass an instance of the AttributedValidatoryFactory into the Model Binder. This means if you have an alternate way of locating the Validator class other than via the attribute you can inherit from IValidatorFactory and create your own.</p>
<p>Now when a parameter of LineItem gets passed into a controller it will be validated against the model and all errors placed into the ModelState. This can then check the isValid property to determine if there are any errors and proceed accordingly.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400"><span style="background: black; color: white"><br />
          </p>
<pre class="code"><span style="background: black; color: white">        [</span><span style="background: black; color: #ffc66d">AcceptVerbs</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">HttpVerbs</span><span style="background: black; color: white">.Post)]
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Edit(</span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt; lineItems)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(ModelState.IsValid)
                </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">RedirectToAction(</span><span style="background: black; color: #a5c25c">&quot;Edit&quot;</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
        }</span></pre>
<p>        </span></td>
</tr>
</tbody>
</table>
<p>And that’s it. I hope you enjoy using the Fluent Validation framework as much as I have and happy coding. For all you guys waiting for xVal integration, I will try and post a solution in the coming week. </p>
</p>
<p>Adam
  </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrating xVal Validation with Linq-to-Sql</title>
		<link>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:58:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[xVal]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</guid>
		<description><![CDATA[In a previous post I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates.
The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. 
After [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous <a href="http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/">post</a> I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates.</p>
<p>The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. </p>
<p>After adding the table to the designer, a Supplier class gets constructed in the background. This class is a partial class which means we can add to it without changing the code auto-generated in the designer.cs file.</p>
<p>We can then create a partial class called Supplier inheriting from Custom Validation and add a MetadataType attribute to it. This attribute specifies the class for which use for validating the Supplier class.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">MetadataType</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">SupplierValidation</span><span style="background: black; color: white">))]
</span><span style="background: black; color: #cc7832">    public partial class </span><span style="background: black; color: #ffc66d">Supplier</span><span style="background: black; color: white"> : </span><span style="background: black; color: #ffc66d">CustomValidation</span><span style="background: black; color: #ffc66d">
</span><span style="background: black; color: white">    {
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>We can then create the SupplierValidation class specifying the properties of the Supplier class we would like to be validated. For instance here I only want to validate the ContactName and the ContactTitle of the Supplier.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">    public class </span><span style="background: black; color: #ffc66d">SupplierValidation
    </span><span style="background: black; color: white">{
        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactName { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }

        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">, </span><span style="background: black; color: #ffc66d">Range</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">10</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactTitle { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This specifies that both fields are required and that the ContactTitle cannot be more than 10 characters in length. </p>
<p>The other benefit of using the buddy class here is that if you need to regenerate a table in the linq-to-sql designer, you won’t lose your changes because they’re contained in a separate file.</p>
<p>From here when a Supplier gets passed to in as a parameter on a Controller action it will be validated using the rules in the Supplier Validation class. </p>
<p>Cheers,<br />
  <br />Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transforming one-to-many Sql into Nested Class</title>
		<link>http://schotime.net/blog/index.php/2009/01/22/transforming-one-to-many-sql-into-nested-class/</link>
		<comments>http://schotime.net/blog/index.php/2009/01/22/transforming-one-to-many-sql-into-nested-class/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 11:13:13 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/01/22/transforming-one-to-many-sql-into-nested-class/</guid>
		<description><![CDATA[Back in the days of Classic ASP, when you had a one too many relationship displaying the data was pretty painful. You would do things like 




        var reference_number_old = &#34;$##%@&#34;;
        while (recordsExist)
        {
 [...]]]></description>
			<content:encoded><![CDATA[<p>Back in the days of Classic ASP, when you had a one too many relationship displaying the data was pretty painful. You would do things like </p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: white">        var reference_number_old = </span><span style="background: black; color: #a5c25c">&quot;$##%@&quot;</span><span style="background: black; color: white">;
        </span><span style="background: black; color: #cc7832">while </span><span style="background: black; color: white">(recordsExist)
        {
            reference_number = String(rs(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">));

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(reference_number_old != reference_number)
            {
                Response.Write(reference_number);
                reference_number_old = reference_number;
            }

            </span><span style="background: black; color: gray">//Do more stuff
        </span><span style="background: black; color: white">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Sooo very very painful, and that&#8217;s only grouping by one column (reference number).</p>
<p>Now however with OO programming and the advance of Linq there is a much nicer and easier way to deal with this Master-Detail type one-to-many relationship.</p>
<p>Lets see how we do it now, still using our own custom sql.<br />
  <br />Firstly here is our class to represent our flat data straight out of the database.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">FlatCustomerAndOrders
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">ReferenceNumber { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Name { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Address { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">OrderId { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ProductName { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Description { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Amount { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Even filling this class used to be a bit painful before Linq. You would have to create a connection, create a reader, then instantiate the FlatCustomerAndOrders class. Set the properties then add the object to a list while records could still be read. Here&#8217;s how I do it now.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #ffc66d">DataContext </span><span style="background: black; color: white">dc = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">DataContext</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">SqlConnection</span><span style="background: black; color: white">(connString));
        </span><span style="background: black; color: #6897bb">IEnumerable</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">FlatCustomerAndOrders</span><span style="background: black; color: white">&gt; flat =
            dc.ExecuteQuery&lt;</span><span style="background: black; color: #ffc66d">FlatCustomerAndOrders</span><span style="background: black; color: white">&gt;(</span><span style="background: black; color: #a5c25c">&quot;select c.referencenumber, c.name,&quot; </span><span style="background: black; color: white">+
            </span><span style="background: black; color: #a5c25c">&quot;c.address, o.orderid, o.productname, o.description, o.amount from customers &quot; </span><span style="background: black; color: white">+
            </span><span style="background: black; color: #a5c25c">&quot;c inner join orders o on c.referencenumber = o.referencenumber&quot;</span><span style="background: black; color: white">);
</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>Thats its. We have our List of FlatCustomerAndOrders.<br />
  <br />The next step is to organise the data so that the data is nested so its easy to display with just two foreach loops. The two classes:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">Customer
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">ReferenceNumber { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Name { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Address { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Order</span><span style="background: black; color: white">&gt; Orders { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        }

        </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">Order
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">OrderId { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ProductName { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Description { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
            </span><span style="background: black; color: #cc7832">public decimal </span><span style="background: black; color: white">Amount { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>To fill these two class we do some clever Linq using the GroupBy method. Here&#8217;s the code.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #6897bb">IList</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Customer</span><span style="background: black; color: white">&gt; Customers =
            flat.GroupBy(cust =&gt; </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ cust.ReferenceNumber, cust.Name, cust.Address })
                .Select(c =&gt; </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Customer</span><span style="background: black; color: white">()
                {
                    ReferenceNumber = c.Key.ReferenceNumber,
                    Name = c.Key.Name,
                    Address = c.Key.Address,
                    Orders = c.Select(o =&gt; </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Order</span><span style="background: black; color: white">()
                    {
                        OrderId = o.OrderId,
                        ProductName = o.ProductName,
                        Description = o.Description,
                        Amount = o.Amount
                    }).ToList()
                }).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>And that&#8217;s it. It may look like a bit of a mouthful but its quite simple really.<br />
  <br />It just takes the flat list and applies the groupby method to it grouping by the three customer columns. It then builds a new list to fill the Orders property with. </p>
<p>Its then super easy to display the data like so.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: white">       </span><span style="background: black; color: #cc7832"> foreach </span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">Customer </span><span style="background: black; color: white">c </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">Customers)
        {
            </span><span style="background: black; color: gray">//Display the customer details

            </span><span style="background: black; color: #cc7832">foreach </span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">Order </span><span style="background: black; color: white">o </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">c.Orders)
            {
                </span><span style="background: black; color: gray">//Display the orders for each customer
            </span><span style="background: black; color: white">}
        }
</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>Well I hope this helps you as much as it has helped me.</p>
<p>Cheers<br />
  <br />Schotime </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/01/22/transforming-one-to-many-sql-into-nested-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Running ASP.NET MVC Applications Under IIS6</title>
		<link>http://schotime.net/blog/index.php/2008/10/18/running-aspnet-mvc-applications-under-iis6/</link>
		<comments>http://schotime.net/blog/index.php/2008/10/18/running-aspnet-mvc-applications-under-iis6/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 09:41:29 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Routing]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2008/10/18/running-aspnet-mvc-applications-under-iis6/</guid>
		<description><![CDATA[Yes&#8230;Finally&#8230;I have full control of my HTML again, but am still able to work with a great language like C#. MVC is here and its great. I&#8217;m absolutely loving it. But how the heck do you get it to work under IIS6. What if I want extensionless URL&#8217;s? Here is a few options for you.
Option [...]]]></description>
			<content:encoded><![CDATA[<p>Yes&#8230;Finally&#8230;I have full control of my HTML again, but am still able to work with a great language like C#. MVC is here and its great. I&#8217;m absolutely loving it. But how the heck do you get it to work under IIS6. What if I want extensionless URL&#8217;s? Here is a few options for you.</p>
<p><strong>Option 1:      <br /></strong>Running IIS with Wildcard Application mapping.</p>
<p>This option allows extensionless URL&#8217;s but a what performance price. Every single request including images, files and css etc. will get passed through the aspnet_isapi.dll. For small sites this may not be a huge issue, but small sites soon because large sites and this then becomes an issue.</p>
<p>I&#8217;m not going to run through how to do this, because there are already some nice blogs about this and I don&#8217;t really recommend this option.</p>
<p><strong>Option 2:      <br /></strong>Running IIS with .mvc (or whatever you like) extensions.</p>
<p>I started out running my site (schotime.net) like this, however in the end I decided on Option 3.    <br />URLs look like this.&#160;&#160; /Home.mvc/Index&#160;&#160; rather than /Home/Index&#160; which is not that bad but again not as nice as the latter.</p>
<p>Anyways here&#8217;s how you set this up.    <br />Firstly your global.asax.cs should look like this.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Collections.Generic;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Linq;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web.Mvc;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web.Routing;

</span><span style="background: black; color: #cc7832">namespace </span><span style="background: black; color: white">MVCApplication1
{
    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">MvcApplication </span><span style="background: black; color: white">: System.Web.HttpApplication
    {
        </span><span style="background: black; color: #cc7832">public static void </span><span style="background: black; color: white">RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute(</span><span style="background: black; color: #a5c25c">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="background: black; color: white">);

            routes.MapRoute(
                </span><span style="background: black; color: #a5c25c">&quot;Default&quot;</span><span style="background: black; color: white">,                                              </span><span style="background: black; color: gray">
                </span><span style="background: black; color: #a5c25c">&quot;{controller}.mvc/{action}/{id}&quot;</span><span style="background: black; color: white">,                       </span><span style="background: black; color: gray">
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Home&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index&quot;</span><span style="background: black; color: white">, id = </span><span style="background: black; color: #a5c25c">&quot;&quot; </span><span style="background: black; color: white">}  </span><span style="background: black; color: gray">
            </span><span style="background: black; color: white">);

            routes.MapRoute(
                </span><span style="background: black; color: #a5c25c">&quot;Defaultest&quot;</span><span style="background: black; color: white">,                                           </span><span style="background: black; color: gray">
                </span><span style="background: black; color: #a5c25c">&quot;Default.aspx&quot;</span><span style="background: black; color: white">,                                         </span><span style="background: black; color: gray">
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Home&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index&quot;</span><span style="background: black; color: white">, id = </span><span style="background: black; color: #a5c25c">&quot;&quot; </span><span style="background: black; color: white">}  </span><span style="background: black; color: gray">
            </span><span style="background: black; color: white">);

        }

        </span><span style="background: black; color: #cc7832">protected void </span><span style="background: black; color: white">Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.RouteExistingFiles = </span><span style="background: black; color: #cc7832">true</span><span style="background: black; color: white">;
        }
    }
}</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>This will get you started. All you need to do then is map the extension .mvc to the aspnet_isapi.dll in IIS under Home Directory -&gt; Configuration -&gt; Mappings or use an already mapped extension like .aspx and your mvc application should work.</p>
<p><strong>Option 3:<br />
    <br /></strong>This option requires the use of a clever isapi filter to rewrite the URLs. </p>
<p>I originally read an article on <a href="http://biasecurities.com/blog/2008/how-to-enable-pretty-urls-with-asp-net-mvc-and-iis6/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/biasecurities.com/blog/2008/how-to-enable-pretty-urls-with-asp-net-mvc-and-iis6/?referer=');">Bia Securities</a> which uses the Isapi_rewrite 3rd party plugin however, Isapi_rewrite is not free so I thought I would find a suitable open source solution. Here it is: Ionic Rewriter -&gt; <a title="http://www.codeplex.com/IIRF" href="http://www.codeplex.com/IIRF" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/IIRF?referer=');">http://www.codeplex.com/IIRF</a> </p>
<p>It works in much the same way as the isapi_rewrite but will a few little differences. Once you download the IsapiRewrite4.dll, place the dll in a directory eg. C:\WINDOWS\system32\inetsrv\IIRF and run through the installation instructions included in the download (readme.txt) under Installation. Next comes the configuration. Firstly here is the IsapiRewrite4.ini configuration file ported from the <a href="http://biasecurities.com/blog/2008/how-to-enable-pretty-urls-with-asp-net-mvc-and-iis6/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/biasecurities.com/blog/2008/how-to-enable-pretty-urls-with-asp-net-mvc-and-iis6/?referer=');">Bia Securities</a> article.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<p>#RewriteLog&#160; c:\temp\iirfLog.out<br />
          <br />#RewriteLogLevel 3 </p>
<p>RewriteRule ^/Default\.aspx /Home.mvc [I,L] </p>
<p>RewriteRule ^/$ /Home.mvc [I,L] </p>
<p>RewriteRule ^/([\w]+)$ /$1.mvc [I,L] </p>
<p>RewriteRule ^/(?!Content)([\w]*)/(.*) /$1.mvc/$2 [I,L]</p>
</td>
</tr>
</tbody>
</table>
<p>The [I,L] stands for a case-insensitive match and to stop processing if the current rule is a match.</p>
<p><strong>Important Note:<br />
    <br /></strong>Any physical files that you directly linked to should not be routed. In my MVC folder system I store all of these files under the /Content directory. Wherever you store your files replace the text Content with the folder that your files reside. otherwise images, css etc. will fail to display. </p>
<p>Now all that&#8217;s left to do is to modify the global.asax.cs to support this. </p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Collections.Generic;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Linq;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web.Mvc;
</span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">System.Web.Routing;

</span><span style="background: black; color: #cc7832">namespace </span><span style="background: black; color: white">MVCApplication1
{
    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">MvcApplication </span><span style="background: black; color: white">: System.Web.HttpApplication
    {
        </span><span style="background: black; color: #cc7832">public static void </span><span style="background: black; color: white">RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute(</span><span style="background: black; color: #a5c25c">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="background: black; color: white">);

            routes.MapRoute(
                </span><span style="background: black; color: #a5c25c">&quot;Default&quot;</span><span style="background: black; color: white">,                                              </span><span style="background: black; color: gray">// Route name
                </span><span style="background: black; color: #a5c25c">&quot;{controller}/{action}/{id}&quot;</span><span style="background: black; color: white">,                           </span><span style="background: black; color: gray">// URL with parameters
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Home&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index&quot;</span><span style="background: black; color: white">, id = </span><span style="background: black; color: #a5c25c">&quot;&quot; </span><span style="background: black; color: white">}, </span><span style="background: black; color: gray">// Parameter defaults
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a31515">@&quot;[^\.]*&quot; </span><span style="background: black; color: white">}
            );

            routes.MapRoute(
                </span><span style="background: black; color: #a5c25c">&quot;Defaultmvc&quot;</span><span style="background: black; color: white">,                                               </span><span style="background: black; color: gray">// Route name
                </span><span style="background: black; color: #a5c25c">&quot;{controller}.mvc/{action}/{id}&quot;</span><span style="background: black; color: white">,                           </span><span style="background: black; color: gray">// URL with parameters
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Home&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index&quot;</span><span style="background: black; color: white">, id = </span><span style="background: black; color: #a5c25c">&quot;&quot; </span><span style="background: black; color: white">},     </span><span style="background: black; color: gray">// Parameter defaults
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a31515">@&quot;[^\.]*&quot; </span><span style="background: black; color: white">}
            );
        }

        </span><span style="background: black; color: #cc7832">protected void </span><span style="background: black; color: white">Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong>&#160; The line with this on it -&gt; new { controller = @&quot;[^\.]*&quot; }&#160;&#160; is very important. The routes won&#8217;t work without it. </p>
<p>And that&#8217;s it! You&#8217;re done.<br />
  <br />Personally I like the last option but if you have no access to your hosting web server then Option 2 is probably the best.</p>
<p>Schotime</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/10/18/running-aspnet-mvc-applications-under-iis6/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Dataset, Datatable to Json</title>
		<link>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/</link>
		<comments>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 05:51:36 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Json]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/</guid>
		<description><![CDATA[After my previous posts about returning data to the client as a JSON object, I decided to have a go at returning a generic Datatable/Dataset. This however is not as easy as simple returning a Datatable in your code behind method or web service. There is a solution though and here it is.
If you break [...]]]></description>
			<content:encoded><![CDATA[<p>After my previous posts about returning data to the client as a JSON object, I decided to have a go at returning a generic Datatable/Dataset. This however is not as easy as simple returning a Datatable in your code behind method or web service. There is a solution though and here it is.</p>
<p>If you break a Datatable down it is really only a List of Dictionary objects so that&#8217;s how we&#8217;ll approach this problem. This is compatible with .NET 2.0 and above, with the Ajax installed.</p>
<p>I&#8217;d like to acknowledge <a href="http://www.trinet.co.uk/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.trinet.co.uk/?referer=');">RichardD</a> for the idea.</p>
<p>Below is the solution.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Data;

<span style="color: blue">public static class </span><span style="color: #2b91af">JsonMethods
</span>{
    <span style="color: blue">private static </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt;
        RowsToDictionary(<span style="color: #2b91af">DataTable </span>table)
    {
        <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt; objs =
            <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt;();
        <span style="color: blue">foreach </span>(<span style="color: #2b91af">DataRow </span>dr <span style="color: blue">in </span>table.Rows)
        {
            <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; drow = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
            <span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i &lt; table.Columns.Count; i++)
            {
                drow.Add(table.Columns[i].ColumnName, dr[i]);
            }
            objs.Add(drow);
        }

        <span style="color: blue">return </span>objs;
    }

    <span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; ToJson(<span style="color: #2b91af">DataTable </span>table)
    {
        <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; d = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
        d.Add(table.TableName, RowsToDictionary(table));
        <span style="color: blue">return </span>d;
    }

    <span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; ToJson(<span style="color: #2b91af">DataSet </span>data)
    {
        <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; d = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
        <span style="color: blue">foreach </span>(<span style="color: #2b91af">DataTable </span>table <span style="color: blue">in </span>data.Tables)
        {
            d.Add(table.TableName, RowsToDictionary(table));
        }
        <span style="color: blue">return </span>d;
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The static class JsonMethods exposes two public static methods and a private method. The public method ToJson() takes either a Dataset or a Datatable, and returns a Dictionary&lt;string,object&gt; object. The key to this class is the RowsToDictionary() method. </p>
<p>This method iterates through all the rows creating a dictionary entry for each column in the row using the column name as the key and storing the data value into the object. It then adds the Dictionary object to a List of Dictionary Objects and returns this to the ToJson() method. This Dictionary list is then added to another Dictionary object using the table name as the key. We&#8217;ll see how this all works together soon.</p>
<p>Lets have a look at the code behind now.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code">[System.Web.Script.Services.<span style="color: #2b91af">ScriptMethod</span>(ResponseFormat = <span style="color: #2b91af">ResponseFormat</span>.Json)]
[System.Web.Services.<span style="color: #2b91af">WebMethod</span>]
<span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; getTable()
{
    <span style="color: blue">string </span>sql = <span style="color: #a31515">&quot;select user_name, active_indicator, create_date from users&quot;</span>;
    <span style="color: blue">string </span>connString = <span style="color: #a31515">&quot;database=db; server=localhost; user id=sa;&quot;</span>;

    <span style="color: blue">return </span><span style="color: #2b91af">JsonMethods</span>.ToJson(GetDataTable(sql, connString));
}

<span style="color: blue">private static </span><span style="color: #2b91af">DataTable </span>GetDataTable(<span style="color: blue">string </span>sql, <span style="color: blue">string </span>connString)
{
    <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>myConnection = <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connString))
    {
        <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>myCommand = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(sql, myConnection))
        {
            myConnection.Open();
            <span style="color: blue">using </span>(<span style="color: #2b91af">SqlDataReader </span>myReader = myCommand.ExecuteReader())
            {
                <span style="color: #2b91af">DataTable </span>myTable = <span style="color: blue">new </span><span style="color: #2b91af">DataTable</span>();
                myTable.TableName = <span style="color: #a31515">&quot;mydt&quot;</span>;
                myTable.Load(myReader);
                myConnection.Close();
                <span style="color: blue">return </span>myTable;
            }
        }
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>So what I have above is two static methods. One is GetTable which is the one we will access from the client. The other is a generic method for loading a results set into a Datatable. Note how I have set the TableName property. You will see why soon.</p>
<p>So using the <a href="http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/" target="_blank">jMsAjax plugin</a> as below will return the following JSON object.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code">$.jmsajax({
    type: <span style="color: #a31515">&quot;POST&quot;</span>,
    url: <span style="color: #a31515">&quot;Default.aspx&quot;</span>,
    method: <span style="color: #a31515">&quot;getTable&quot;</span>,
    data: {},
    dataType: <span style="color: #a31515">&quot;msjson&quot;</span>,
    success: <span style="color: blue">function</span>(data) {
        $(outputDT(data.mydt)).appendTo(<span style="color: #a31515">&quot;body&quot;</span>);
    }
});</pre>
</td>
</tr>
</tbody>
</table>
<p>Results (data):<br />
  </p>
<table cellspacing="0" cellpadding="2" width="640" border="1">
<tbody>
<tr>
<td valign="top" width="638">
<p><span style="font-size: 11px; font-family: courier new">{&quot;mydt&quot;:{&quot;user_name&quot;:&quot;000001&quot;,&quot;active_indicator&quot;:&quot;Y&quot;,&quot;create_date&quot;:&quot;\/Date(1170892765197)\/&quot;}, {&quot;user_name&quot;:&quot;000002&quot;,&quot;active_indicator&quot;:&quot;Y&quot;,&quot;create_date&quot;:&quot;\/Date(1170892765197)\/&quot;}]}</span> </p>
</td>
</tr>
</tbody>
</table>
<p>In the resulting data, the table name is the key to referencing the array of values. In this case we use &#8216;mydt&#8217; as the key. In the success function on the client request you may also notice an outputDT function. This is a little helper function which takes a JSON Datatable and returns a the results in a table. This is very useful for debugging. Here is the client side code.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: blue">function </span>outputDT(dataTable)
{
    <span style="color: blue">var </span>headers = [];
    <span style="color: blue">var </span>rows = [];

    headers.push(<span style="color: #a31515">&quot;&lt;tr&gt;&quot;</span>);
    <span style="color: blue">for </span>(<span style="color: blue">var </span>name <span style="color: blue">in </span>dataTable[0])
        headers.push(<span style="color: #a31515">&quot;&lt;td&gt;&lt;b&gt;&quot;</span>+name+<span style="color: #a31515">&quot;&lt;/b&gt;&lt;/td&gt;&quot;</span>);
    headers.push(<span style="color: #a31515">&quot;&lt;/tr&gt;&quot;</span>);

    <span style="color: blue">for </span>(<span style="color: blue">var </span>row <span style="color: blue">in </span>dataTable)
    {
        rows.push(<span style="color: #a31515">&quot;&lt;tr&gt;&quot;</span>);
        <span style="color: blue">for </span>(<span style="color: blue">var </span>name <span style="color: blue">in </span>dataTable[row])
        {
            rows.push(<span style="color: #a31515">&quot;&lt;td&gt;&quot;</span>);
            rows.push(dataTable[row][name]);
            rows.push(<span style="color: #a31515">&quot;&lt;/td&gt;&quot;</span>);
        }
        rows.push(<span style="color: #a31515">&quot;&lt;/tr&gt;&quot;</span>);
    }            

    <span style="color: blue">var </span>top = <span style="color: #a31515">&quot;&lt;table border='1'&gt;&quot;</span>;
    <span style="color: blue">var </span>bottom = <span style="color: #a31515">&quot;&lt;/table&gt;&quot;</span>;  

    <span style="color: blue">return </span>top + headers.join(<span style="color: #a31515">&quot;&quot;</span>) + rows.join(<span style="color: #a31515">&quot;&quot;</span>) + bottom;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>So as you can see, its now very easy to return a Datatable or Dataset as a JSON object ready for use on the client.</p>
<p>Hope this is as useful for you as it is for me.</p>
<p>Cheers,<br />
  <br />Schotime </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>jQuery Plugin for ASP.net Ajax (jMsAjax)</title>
		<link>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/</link>
		<comments>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 13:25:49 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/</guid>
		<description><![CDATA[After my recent post on jQuery, JSON and dates in asp.net I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.
It not only accepts a raw JSON object as input for method parameters, but safely [...]]]></description>
			<content:encoded><![CDATA[<p>After my recent post on <a href="http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/">jQuery, JSON and dates in asp.net</a> I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.</p>
<p>It not only accepts a raw JSON object as input for method parameters, but safely parses and stringify&#8217;s the object using Crockford&#8217;s implementation (<a href="http://www.json.org/js.html" onclick="pageTracker._trackPageview('/outgoing/www.json.org/js.html?referer=');">http://www.json.org/js.html</a>). The syntax of the call doesn&#8217;t change if it is a &#8220;POST&#8221; or a &#8220;GET&#8221; as it is all handled inside the plugin. It also returns Dates without a problem.</p>
<p>I have add a Home page for the plugin at <a href="http://schotime.net/jMsAjax.aspx">http://schotime.net/jMsAjax.aspx</a> and also added it to the plugins pages on the jQuery website (<a title="http://plugins.jquery.com/project/jMsAjax" href="http://plugins.jquery.com/project/jMsAjax" onclick="pageTracker._trackPageview('/outgoing/plugins.jquery.com/project/jMsAjax?referer=');">http://plugins.jquery.com/project/jMsAjax</a>).</p>
<p>Here is the basic syntax:</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code">$.jmsajax({
    type: <span style="color: #a31515;">"POST"</span>,
    url: <span style="color: #a31515;">"jMsAjax.aspx"</span>,
    method: <span style="color: #a31515;">"getTime"</span>,
    data: { date_in: <span style="color: blue;">new </span>Date() },
    success: <span style="color: blue;">function</span>(data) {
        $(<span style="color: #a31515;">"#div"</span>).html(String(data));
    }
});</pre>
</td>
</tr>
</tbody>
</table>
<p>You can find more information, a demo and the download at <a href="http://schotime.net/jMsAjax.aspx">http://schotime.net/jMsAjax.aspx<br />
</a><strong>Update:</strong> Just fixed the download page. Sorry for the inconvenience.</p>
<p>And at only 4kb + jQuery its significantly less size than MsAjax.</p>
<p>I hope this is as helpful to you as it is for me.</p>
<p>Schotime</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery, AJAX, ASP.NET and Dates</title>
		<link>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/</link>
		<comments>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 09:30:01 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/</guid>
		<description><![CDATA[Just recently I discovered jQuery and I have to say&#8230;.I&#8217;m a huge fan already. So I decided to setup my first AJAX call through jQuery and call a page method. After some playing around I finally had it. Thanks also go out to www.encosia.com. Thanks Dave. The only problem was that I could not parse [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently I discovered jQuery and I have to say&#8230;.I&#8217;m a huge fan already. So I decided to setup my first AJAX call through jQuery and call a page method. After some playing around I finally had it. Thanks also go out to <a href="http://www.encosia.com" onclick="pageTracker._trackPageview('/outgoing/www.encosia.com?referer=');">www.encosia.com</a>. Thanks Dave. The only problem was that I could not parse Dates.</p>
<p style="padding-left: 30px;"><strong>Updated 04/07/08: </strong>Please see my <a href="http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/" target="_blank">latest post</a> for a jQuery plugin that works perfectly with Asp.net. Dates and all.</p>
<p>So here I show you my solution which works pretty well and suits my needs. Maybe you might find it useful.</p>
<p>Firstly I&#8217;ll show you the aspx page and code behind use to make the AJAX call.<br />
<strong>ASPX Page:</strong></p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: #a31515;">html</span><span style="color: blue;">&gt;
&lt;</span><span style="color: #a31515;">head</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">title</span><span style="color: blue;">&gt;</span>My jQuery Test<span style="color: blue;">&lt;/</span><span style="color: #a31515;">title</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">script </span><span style="color: red;">src</span><span style="color: blue;">="jquery.min.js" </span><span style="color: red;">type</span><span style="color: blue;">="text/javascript"&gt;&lt;/</span><span style="color: #a31515;">script</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">script </span><span style="color: red;">type</span><span style="color: blue;">="text/javascript"&gt;
      </span>$(document).ready(<span style="color: blue;">function</span>() {
            $(<span style="color: #a31515;">"#mybutton"</span>).click(<span style="color: blue;">function</span>() {
                  $.ajax({
                      type: <span style="color: #a31515;">"POST"</span>,
                      url: <span style="color: #a31515;">"Default.aspx/jQueryTest"</span>,
                      contentType: <span style="color: #a31515;">"application/json; charset=utf-8"</span>,
                      data: <span style="color: #a31515;">"{ 'dt_in': "</span>+(<span style="color: blue;">new </span>Date()).toMSJSON()+<span style="color: #a31515;">" }"</span>,
                      success: <span style="color: blue;">function</span>(response) {
                        <span style="color: blue;">var </span>data = parseMSJSONString(response);
                        alert(data.d.name);
                        alert(data.d.dt);
                      }
                  });
             });
        });
    <span style="color: blue;">&lt;/</span><span style="color: #a31515;">script</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">head</span><span style="color: blue;">&gt;
&lt;</span><span style="color: #a31515;">body</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">form </span><span style="color: red;">id</span><span style="color: blue;">="form1" </span><span style="color: red;">runat</span><span style="color: blue;">="server"&gt;
    &lt;</span><span style="color: #a31515;">div</span><span style="color: blue;">&gt;
        &lt;</span><span style="color: #a31515;">input </span><span style="color: red;">type</span><span style="color: blue;">="button" </span><span style="color: red;">id</span><span style="color: blue;">="mybutton" </span><span style="color: red;">value</span><span style="color: blue;">="GetDate" /&gt;
    &lt;/</span><span style="color: #a31515;">div</span><span style="color: blue;">&gt;
    &lt;/</span><span style="color: #a31515;">form</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">body</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">html</span><span style="color: blue;">&gt;  </span></pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Code Behind:</strong></p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">public partial class </span><span style="color: #2b91af;">_Default </span>: System.Web.UI.<span style="color: #2b91af;">Page
</span>{
    <span style="color: blue;">protected void </span>Page_Load(<span style="color: blue;">object </span>sender, <span style="color: #2b91af;">EventArgs </span>e)
    {
    }

    [System.Web.Script.Services.<span style="color: #2b91af;">ScriptMethod</span>(ResponseFormat = <span style="color: #2b91af;">ResponseFormat</span>.Json)]
    [System.Web.Services.<span style="color: #2b91af;">WebMethod</span>]
    <span style="color: blue;">public static </span><span style="color: #2b91af;">Test </span>jQueryTest(<span style="color: #2b91af;">DateTime </span>dt_in)
    {
        <span style="color: #2b91af;">Test </span>t = <span style="color: blue;">new </span><span style="color: #2b91af;">Test</span>() { name = <span style="color: #a31515;">"My Name"</span>, dt = dt_in.AddDays(5) };
        <span style="color: blue;">return </span>t;
    }

    <span style="color: blue;">public class </span><span style="color: #2b91af;">Test
    </span>{
        <span style="color: blue;">public string </span>name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
        <span style="color: blue;">public </span><span style="color: #2b91af;">DateTime </span>dt { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong> I have not set a dataType setting of &#8216;json&#8217; in the Ajax call to prevent the JSON string being evaluated into a JSON object before I can parse it. Also note that the name of the parameter passed in through the JSON string data must be the same as the static WebMethod parameter name. In this case &#8220;dt_in&#8221;.</p>
<p>So what we have above is a simple input button with the value of &#8220;GetDate&#8221; which is going to send the current date from JavaScript to the WebMethod, add 5 days to the Date, then add it to a custom class called Test and return an instance of it. At that point the Name and Date will be alerted to the screen, all without a PostBack.</p>
<p>Now usually this would not work however I have created to handy scripts to make it pretty seamless. One to convert a JavaScript Date object to a Date String that ASP.NET will accept, and the other to parse the JSON string returned into a JavaScript Date. Here are those two functions.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code">Date.prototype.toMSJSON = <span style="color: blue;">function </span>() {
      <span style="color: blue;">var </span>date = <span style="color: #a31515;">'"\\\/Date(' </span>+ <span style="color: blue;">this</span>.getTime() + <span style="color: #a31515;">')\\\/"'</span>;
      <span style="color: blue;">return </span>date;
};

<span style="color: blue;">function </span>parseMSJSONString(data)
{
    <span style="color: blue;">try </span>{
        <span style="color: blue;">var </span>newdata = data.replace(
            <span style="color: blue;">new </span>RegExp(<span style="color: #a31515;">'"\\\\\/Date\\\((-?[0-9]+)\\\)\\\\\/"'</span>, <span style="color: #a31515;">"g"</span>)
                        , <span style="color: #a31515;">"new Date($1)"</span>);
        newdata = eval(<span style="color: #a31515;">'('</span>+newdata+<span style="color: #a31515;">')'</span>);
        <span style="color: blue;">return </span>newdata;
    }
    <span style="color: blue;">catch</span>(e) { <span style="color: blue;">return null</span>; }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The first of these two functions toMSJSON extends the Date object by adding the toMSJSON method to it. By doing this any Date object created in JavaScript can be converted to its MS JSON equivalent ready to be sent via Ajax.</p>
<p>The second one is a plain method used to parse the JSON string returned by the WebMethod to a JSON string that is &#8216;evaled&#8217; and made a proper JSON object. Thereby making data.d.dt in the example above a Date object.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Slow TcpClient Connection (sockets)</title>
		<link>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/</link>
		<comments>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/#comments</comments>
		<pubDate>Tue, 27 May 2008 03:25:48 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Sockets]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/05/27/slow-tcpclient-connection-sockets/</guid>
		<description><![CDATA[Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.
After managing to get some lines of communication going between the client and server applications, I couldn&#8217;t help but notice that it was taking a little bit longer than it probably should have. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.</p>
<p>After managing to get some lines of communication going between the client and server applications, I couldn&#8217;t help but notice that it was taking a little bit longer than it probably should have. I decided to have a closer look. </p>
<p>Using some basic timestamping in the console/website application the whole process was taking just over 1 second. This was to connect, send and then receive a response. Further investigation revealed only 1 statement was causing the time to blow out. Below is the statement.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: #2b91af">TcpClient </span>socketForServer = <span style="color: blue">new </span><span style="color: #2b91af">TcpClient</span>(<span style="color: #a31515">"localhost"</span>, 200);</pre>
</td>
</tr>
</tbody>
</table>
<p>Now in all the demos and tutorials this is how it showed to connect to the server. Pretty straightforward and simple. Or so I thought. Whilst having a look through the methods associated with the TcpClient, there was one that stood out: Connect(). Hmmm&#8230;this also took a host name and a port. I decided to give it a try.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: #2b91af">TcpClient </span>socketForServer = <span style="color: blue">new </span><span style="color: #2b91af">TcpClient</span>();
socketForServer.Connect(<span style="color: #a31515">"localhost"</span>, 200);</pre>
</td>
</tr>
</tbody>
</table>
<p>I bet you&#8217;re wondering right now. What is the difference?? At this point in time I&#8217;m not quite sure but I&#8217;m determined to figure it out because after making that change the time to execute the previous two statements was about 1 millisecond or so. Much better.</p>
<p>One thing that did seem odd was if the TcpClient failed to make a connection, the response time was almost exactly the same (~1sec). </p>
<p>If anyone knows the reason, please leave a comment. Until then, two lines will have to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Binding A String to a Checkbox</title>
		<link>http://schotime.net/blog/index.php/2008/04/02/binding-a-string-to-a-checkbox/</link>
		<comments>http://schotime.net/blog/index.php/2008/04/02/binding-a-string-to-a-checkbox/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 07:11:04 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Custom Control]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/04/02/binding-a-string-to-a-checkbox/</guid>
		<description><![CDATA[In a recent project I was attempting to list data from a configuration settings database table. The values were boolean but stored as a &#8216;Y&#8217; or a &#8216;N&#8217; so that only one set of SQL&#8217;s was needed to be written, as the product supports both Oracle and SQL Server. 
When I first put the GridView [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: red"></span>In a recent project I was attempting to list data from a configuration settings database table. The values were boolean but stored as a &#8216;Y&#8217; or a &#8216;N&#8217; so that only one set of SQL&#8217;s was needed to be written, as the product supports both Oracle and SQL Server. </p>
<p>When I first put the GridView together, I was surprised to find that there was no Value property for the asp:checkbox nor could you bind any other column in a database but a &#8216;bit&#8217; column to it.</p>
<p>The only solution I could come up with was to create a custom Checkbox and implement the feature. Below is the code for the custom Checkbox. </p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 500px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">using</span> System;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">using</span> System.ComponentModel;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">using</span> System.Web.UI;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">using</span> System.Web.UI.WebControls;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">namespace</span> ServerControls</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    [Bindable(<span style="color: #0000ff">true</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    [Category(<span style="color: #006080">"Appearance"</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    [DefaultValue(<span style="color: #006080">""</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    [Localizable(<span style="color: #0000ff">true</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    [ToolboxData(<span style="color: #006080">"&lt;{0}:CheckBoxValue runat=server&gt;&lt;/{0}:CheckBoxValue&gt;"</span>)]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CheckBoxValue : CheckBox</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _value;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _unCheckedValue;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _checkedValue;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Value</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            get { <span style="color: #0000ff">return</span> _value; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            set</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                _value = <span style="color: #0000ff">value</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                <span style="color: #0000ff">if</span> (<span style="color: #0000ff">value</span> != <span style="color: #0000ff">null</span> &amp;&amp; <span style="color: #0000ff">this</span>.CheckedValue != <span style="color: #0000ff">null</span> &amp;&amp; <span style="color: #0000ff">this</span>.UnCheckedValue != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                    <span style="color: #0000ff">base</span>.Checked = <span style="color: #0000ff">value</span> == <span style="color: #0000ff">this</span>.CheckedValue;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> CheckedValue</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            get { <span style="color: #0000ff">return</span> _checkedValue; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            set { _checkedValue = <span style="color: #0000ff">value</span>; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> UnCheckedValue</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            get { <span style="color: #0000ff">return</span> _unCheckedValue; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            set { _unCheckedValue = <span style="color: #0000ff">value</span>; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Checked</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.Checked; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            set</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                <span style="color: #0000ff">base</span>.Checked = <span style="color: #0000ff">value</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                <span style="color: #0000ff">if</span> (<span style="color: #0000ff">this</span>.Value != <span style="color: #0000ff">null</span> &amp;&amp; <span style="color: #0000ff">this</span>.CheckedValue != <span style="color: #0000ff">null</span> &amp;&amp; <span style="color: #0000ff">this</span>.UnCheckedValue != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    <span style="color: #0000ff">this</span>.Value = <span style="color: #0000ff">value</span> ? <span style="color: #0000ff">this</span>.CheckedValue : <span style="color: #0000ff">this</span>.UnCheckedValue;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInit(EventArgs e)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Page.RegisterRequiresControlState(<span style="color: #0000ff">this</span>);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">base</span>.OnInit(e);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">object</span> SaveControlState()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">object</span>[] state = <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[2];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            state[0] = <span style="color: #0000ff">base</span>.SaveControlState();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            state[1] = <span style="color: #0000ff">this</span>.Value;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> state;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&nbsp;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> LoadControlState(<span style="color: #0000ff">object</span> state)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">object</span>[] stateTmp = (<span style="color: #0000ff">object</span>[])state;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">base</span>.LoadControlState(stateTmp[0]);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">this</span>.Value = (<span style="color: #0000ff">string</span>)stateTmp[1];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
</div>
</div>
<p>The magic in the custom checkbox occurs in the Value property and the overridden Checked property. </p>
<p>When the value property is set, if it is the same as the &#8216;CheckedValue&#8217; property then we set the base.Checked value to true else false. The same principles apply to the overridden Checked property. When the property is set we set the Value property to the &#8216;CheckedValue&#8217; property if Checked equals true else we set it to the &#8216;UnCheckedValue&#8217;. </p>
<p>Once compiled we can then use this inside our GridView template and bind a varchar column from a database to it. Here is the syntax.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Custom</span><span style="color: blue">:</span><span style="color: #a31515">CheckBoxValue
        </span><span style="color: red">ID</span><span style="color: blue">="cbv1"
        </span><span style="color: red">runat</span><span style="color: blue">="server"
        </span><span style="color: red">CheckedValue</span><span style="color: blue">="Y"
        </span><span style="color: red">UnCheckedValue</span><span style="color: blue">="N"
        </span><span style="color: red">Value</span><span style="color: blue">='</span><span style="background: #ffee62">&lt;%</span># Bind("valueyn") <span style="background: #ffee62">%&gt;</span><span style="color: blue">' /&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
<p><a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a>So when the GridView databinds and evaluates the &#8216;valueyn&#8217; column; if the value is &#8216;Y&#8217; then the checkbox will be checked and if the value is &#8216;N&#8217; then the checkbox will be unchecked. Then if I choose to edit a line with the checkbox and change its value, the value stored in the database will match the value of the checkbox. eg. if the checkbox is checked, &#8216;Y&#8217; will be entered into the database and if it is left unchecked then &#8216;N&#8217; will be entered. </p>
<p>This seems to work extremely well for my project and suits my needs. It will even work if the &#8216;valueyn&#8217; column is an Int, as long as you modify the &#8216;CheckedValue&#8217; and &#8216;UnCheckedValue&#8217; to something that can be implicitly converted to an Int.</p>
<p>Hope this helps. If you find any issues or have other ideas please don&#8217;t hesitate to leave a comment. </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/04/02/binding-a-string-to-a-checkbox/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
