<?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>Quickduck &#187; Asp.Net</title> <atom:link href="http://quickduck.com/blog/category/development/net/aspnet/feed/" rel="self" type="application/rss+xml" /><link>http://quickduck.com/blog</link> <description>Straight from the mind of geniuseseses....</description> <lastBuildDate>Mon, 09 Jan 2012 02:29:30 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>MVC 3 XML Sitemap</title><link>http://quickduck.com/blog/2012/01/08/mvc-3-xml-sitemap/</link> <comments>http://quickduck.com/blog/2012/01/08/mvc-3-xml-sitemap/#comments</comments> <pubDate>Sun, 08 Jan 2012 01:24:36 +0000</pubDate> <dc:creator>Drew Freyling</dc:creator> <category><![CDATA[.Net]]></category> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[Xml]]></category> <category><![CDATA[seo]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=425</guid> <description><![CDATA[Ok, so you&#8217;ve got your shiny new mvc3 app up and running. Now, it&#8217;s time to bake in some of that SEO goodness. Here&#8217;s how you can easily add a sitemap.xml to your new mvc application. The solution specified below not only pumps out the xml file to submit to the search engines, but it [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2012/01/08/mvc-3-xml-sitemap/"></g:plusone></div><p>Ok, so you&#8217;ve got your shiny new mvc3 app up and running. Now, it&#8217;s time to bake in some of that SEO goodness. Here&#8217;s how you can easily add a sitemap.xml to your new mvc application. The solution specified below not only pumps out the xml file to submit to the search engines, but it is also a System.Web.SiteMapProvider, so you can use it for menus and wherever else you want to use your sitemap.</p><p>I&#8217;m a big fan of NuGet, so bring up the Package Manager Console and grab MvcSiteMapProvider (or download it from <a title="MvcSiteMapProvider" href="https://github.com/maartenba/MvcSiteMapProvider" target="_blank">github</a>).</p><pre class="brush: plain; title: ; notranslate">Install-Package MvcSiteMapProvider</pre><p>Once it is installed there will be a new Mvc.sitemap file added with the following to your solution:</p><pre class="brush: xml; title: ; notranslate">&lt;mvcSiteMapNode title=&quot;Home&quot; controller=&quot;Home&quot; action=&quot;Index&quot;&gt;
    &lt;mvcSiteMapNode title=&quot;About&quot; controller=&quot;Home&quot; action=&quot;About&quot;/&gt;
&lt;/mvcSiteMapNode&gt;</pre><p>Now for the easy bit: to get a ~/sitemap.xml to auto-generate for us based on our controller actions we specified in the sitemap file above. All you need to do is simply add the following to your route registration (generally Global.Application_Start()):</p><pre class="brush: csharp; title: ; notranslate">
XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
</pre><p>Now, like a lot of other people out there my next question was, what about my dynamically generated content from the database (such as Product/Details/1)?  Easy &#8211; MvcSiteMapProvider comes with dynamic node support. Just create a class like the following:</p><pre class="brush: csharp; title: ; notranslate">

public class ProductDetailsDynamicNodeProvider : DynamicNodeProviderBase
 {
     public override IEnumerable&lt;DynamicNode&gt; GetDynamicNodeCollection()
     {
         // Create a node for each product
         foreach (Product product in productService.GetAllProducts())
         {
             var node = new DynamicNode();
             node.Title = product.Name;
             node.RouteValues.Add(&quot;id&quot;, product.Id);

             yield return node;
         }
     }
     public override CacheDescription GetCacheDescription()
     {
        return new CacheDescription(&quot;ProductDetailsDynamicNodeProvider&quot;)
        {
            SlidingExpiration = TimeSpan.FromMinutes(60);
        };
    }
 }
</pre><p>Things to note with the above code: you don&#8217;t have to override the cachedescription method, but this does allow us to specify how long all our products will be cached for in the sitemap. Then, you can add your dynamic node to the mvc.sitemap files as follows:</p><pre class="brush: xml; title: ; notranslate">
&lt;mvcSiteMapNode title=&quot;Details&quot; action=&quot;Details&quot;
dynamicNodeProvider=&quot;Website.ProductDetailsDynamicNodeProvider, Website&quot;/&gt;
</pre><p>Now you are done and have a working auto-generated mvc sitemap. For those of us who are using it to generate breadcrumbs, check out the awesome example of <a href="https://github.com/maartenba/MvcSiteMapProvider/wiki/HtmlHelper-functions" target="_blank">html helpers in the documentation</a>.</p><p>Next time, I&#8217;ll be looking at creating SEO friendly URLs in MVC.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2012/01/08/mvc-3-xml-sitemap/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Overriding the default serialization behavior in Json.NET</title><link>http://quickduck.com/blog/2011/08/08/overriding-the-default-serialization-behavior-in-json-net/</link> <comments>http://quickduck.com/blog/2011/08/08/overriding-the-default-serialization-behavior-in-json-net/#comments</comments> <pubDate>Mon, 08 Aug 2011 06:19:11 +0000</pubDate> <dc:creator>Gerrod</dc:creator> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[C#]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=412</guid> <description><![CDATA[We use Newtonsoft Json.NET heavily in our project to control our JSON serialization. For the most part, it works absolutely perfectly out-of-the-box, but every now and then, we need to customize the way that serialization works for a particular object. Such an occasion occurred today. Basically, I have a list of EmployeeActivity objects &#8211; where [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2011/08/08/overriding-the-default-serialization-behavior-in-json-net/"></g:plusone></div><p>We use <a href="http://json.codeplex.com/">Newtonsoft Json.NET</a> heavily in our project to control our JSON serialization. For the most part, it works absolutely perfectly out-of-the-box, but every now and then, we need to customize the way that serialization works for a particular object.</p><p>Such an occasion occurred today. Basically, I have a list of <code>EmployeeActivity</code> objects &#8211; where each instance is a <em>subclass</em> of <code>EmployeeActivity</code>. So, for example:</p><pre class="brush: csharp; title: ; notranslate">
public abstract class EmployeeActivity
{
    public TimeSpan Start { get; set; }
    public TimeSpan Stop { get; set; }

    public virtual bool IsAvailable
    {
        get { return false; }
    }
}

public class AbsenceActivity : EmployeeActivity
{
    public string Reason { get; set; }
}

public class BreakActivity : EmployeeActivity
{
    public string BreakName { get; set; }
}
</pre><p>json.NET does a perfect job of serializing my objects, and they rebuild perfectly when I&#8217;m back in javascript land &#8211; however, I have no way of knowing the actual <em>type</em> of each activity in my array! To alleviate this, I wanted to create my own serializer which also injected the type name into each of the serialized objects &#8211; or at least, that&#8217;s what I <em>thought</em> I wanted to do!</p><p>As it turns out, there&#8217;s a bit of an easier option. <code>JsonConvert</code> uses a <a href="http://james.newtonking.com/projects/json/help/ContractResolver.html"><code>ContractResolver</code></a> in order to work out how to Serialize/Deserialize each class, and you can override the default contract resolver when running serialization:</p><pre class="brush: csharp; title: ; notranslate">
// This class contains the activity list
ActivityViewModel = new ActivityViewModel(DateTime.Today);

// Serialize using a custom contract resolver
string jsonViewModel = JsonConvert.SerializeObject(viewModel,
    Formatting.None, new JsonSerializerSettings
    {
        ContractResolver = new ActivityJsonContractResolver()
    });
</pre><p>The contract resolver&#8217;s job is to return a <a href="http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_Serialization_JsonContract.htm"><code>JsonContract</code></a> which describes the object to be returned. For a POCO, this would typically be an instance of <a href="http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_Serialization_JsonObjectContract.htm"><code>JsonObjectContract</code></a>, which (amongst other things) describes the properties of the class which should be serialized.</p><p>So, all I needed to do was to extend the contract for subclasses of <code>EmployeeActivity</code>, such that it inherited a new Property to be serialized:</p><pre class="brush: csharp; title: ; notranslate">
public class DiaryActivityJsonContractResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        JsonObjectContract contract = base.CreateObjectContract(objectType);

        if (typeof(EmployeeActivity).IsAssignableFrom(objectType))
        {
            contract.Properties.Add(new JsonProperty
            {
                Readable = true,
                ShouldSerialize = value =&gt; true,
                PropertyName = &quot;Type&quot;,
                PropertyType = typeof(string),
                Converter = ResolveContractConverter(typeof(string)),
                ValueProvider = new StaticValueProvider(objectType.Name)
            });
        }

        return contract;
    }
}
</pre><p>Truth be told, I&#8217;m not 100% sure if I need to implement as many of the properties on the <code>JsonProperty</code> that I&#8217;m creating. But you can see what&#8217;s going on here &#8211; I&#8217;m effectively telling the object contract that there&#8217;s an additional property called <code>Type</code> which needs to be serialized, that it&#8217;s a string, and that it should return whatever value is resolved by the <code>StaticValueProvider</code>. Speaking of which, here&#8217;s the last piece of the puzzle &#8211; a simple value resolver that always returns the same value:</p><pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// JSON value provider that always returns a static value
/// &lt;/summary&gt;
public class StaticValueProvider : IValueProvider
{
    private readonly object _staticValue;

    public StaticValueProvider(object staticValue)
    {
        _staticValue = staticValue;
    }

    public void SetValue(object target, object value)
    {
        throw new NotSupportedException();
    }

    public object GetValue(object target)
    {
        return _staticValue;
    }
}
</pre><p>json.NET is fantastic, and it&#8217;s very extensible, but finding out where to start and what to do can be a little bit tricky. I managed to figure this out only by downloading the source files and poking through the code myself; hopefully this will be helpful to someone else in the same situation!</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2011/08/08/overriding-the-default-serialization-behavior-in-json-net/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery Intellisense Support in Content Pages</title><link>http://quickduck.com/blog/2011/02/03/jquery-intellisense-support-in-content-pages/</link> <comments>http://quickduck.com/blog/2011/02/03/jquery-intellisense-support-in-content-pages/#comments</comments> <pubDate>Wed, 02 Feb 2011 21:27:17 +0000</pubDate> <dc:creator>Drew Freyling</dc:creator> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[.Net]]></category> <category><![CDATA[jQuery]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=335</guid> <description><![CDATA[I was trying my hand at whipping up some jQuery today and realising the awesome intellisense that is provided, couldn&#8217;t wait to get stuck in. Since I do enjoy adhering to the DRY principle wherever possible I decided I needed to use master pages first before I create my example content. I then set out [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2011/02/03/jquery-intellisense-support-in-content-pages/"></g:plusone></div><p>I was trying my hand at whipping up some jQuery today and realising the awesome <a href="http://weblogs.asp.net/scottgu/archive/2008/11/21/jquery-intellisense-in-vs-2008.aspx">intellisense</a> that is provided, couldn&#8217;t wait to get stuck in. Since I do enjoy adhering to the DRY principle wherever possible I decided I needed to use master pages first before I create my example content. I then set out to try and do a basic hello world example but realized my intellisense wasn&#8217;t working with jQuery. It was working with vanilla ASP .NET pages, just not content pages.</p><p>It turns out, that VS uses -vsdocs javascript files to provide this support. It requires it to be sitting next to your jquery file that you are referencing. Which it was thanks to the MVC Internet Application project template. It seems that ASP .NET doesn&#8217;t know at design time that I&#8217;m referencing jQuery in the master page.</p><p>After asking Professor Google, I stumbled upon a <a href="http://stackoverflow.com/questions/770810/how-do-you-get-jquery-intellisense-working-if-youve-implemented-a-url-helper-ext" target="_blank">solution</a>. I decided to slap the following bit of code into any content page that I have created and want some good ol&#8217; intellisense.</p><pre class="brush: xml; title: ; notranslate">
&lt;% #if (false) %&gt;
&lt;script src=&quot;~/scripts/jquery-1.4.4.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;% #endif %&gt;
</pre><p>Yes, this is a bit of hack to get it working and yes, I would like Microsoft to fix it. In the mean time, I will be egarly awaiting the <a href="http://appendto.com/community/jquery-vsdoc">intellisense support to be released</a> for the latest 1.5 version.</p><p><strong>UPDATE:</strong> I&#8217;ve just found that somebody has generated a <a href="http://encosia.com/2011/02/04/a-vsdoc-for-jquery-1-5/">new vsdoc for jQuery 1.5</a></p><p><strong>UPDATE 2</strong>: If you reference your jQuery from the Microsoft CDN, you get intellisense since they also host the vsdoc files. Sadly, the Google CDN does not.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2011/02/03/jquery-intellisense-support-in-content-pages/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>DateTime Localization in ASP .NET</title><link>http://quickduck.com/blog/2010/06/18/datetime-localization-in-asp-net/</link> <comments>http://quickduck.com/blog/2010/06/18/datetime-localization-in-asp-net/#comments</comments> <pubDate>Fri, 18 Jun 2010 08:11:25 +0000</pubDate> <dc:creator>Drew Freyling</dc:creator> <category><![CDATA[.Net]]></category> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[C#]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=298</guid> <description><![CDATA[You&#8217;ve got an ASP .NET app and you need to display a date. Normally that just involves the old DateTime.ToShortDateString() or DateTime.ToString(&#8220;d&#8221;) and you continue coding on your merry little way. But what happens when your users are from different countries and expect different date formats.  Between Australia, USA and Japan there are 3 different [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2010/06/18/datetime-localization-in-asp-net/"></g:plusone></div><p>You&#8217;ve got an ASP .NET app and you need to display a date. Normally that just involves the old DateTime.ToShortDateString() or DateTime.ToString(&#8220;d&#8221;) and you continue coding on your merry little way. But what happens when your users are from different countries and expect different date formats.  Between Australia, USA and Japan there are 3 different date formats.</p><p>Then answer? The HttpRequest&#8217;s UserLanguages. This string array basically maps to the following languages that you can setup in your browser:</p><p><a href="http://quickduck.com/blog/wp-content/uploads/2010/06/screenshot.png"><img class="alignnone size-full wp-image-301" title="languages" src="http://quickduck.com/blog/wp-content/uploads/2010/06/screenshot.png" alt="" width="592" height="465" /></a></p><p>Now for the code:</p><pre class="brush: csharp; title: ; notranslate">

CultureInfo culture = Request.UserLanguages != null
? CultureInfo.CreateSpecificCulture(Request.UserLanguages[0])
: CultureInfo.CurrentCulture;
myLabel.Text = DateTimeOffset.Now.ToLocalTime().ToString(&quot;d&quot;, culture);
</pre><div>Don&#8217;t forget to check if the UserLanguages is not null since you can remove all the values from the list if you like.</div><div>Also, if you are looking at ways to do this client side, ScottGu&#8217;s Blog has a nice <a title="jQuery Globalization Plugin from Microsoft" href="http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx" target="_blank">post on a new jquery plugin</a> for this.</div><p><strong>* Update *</strong><br /> Just realised that you don&#8217;t need to any of this if you use the following in your web.config:</p><pre class="brush: xml; title: ; notranslate">
&lt;globalization uiCulture=&quot;auto&quot; culture=&quot;auto&quot; /&gt;
</pre>]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2010/06/18/datetime-localization-in-asp-net/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Path resolution in ASP.NET</title><link>http://quickduck.com/blog/2010/01/14/path-resolution-in-asp-net/</link> <comments>http://quickduck.com/blog/2010/01/14/path-resolution-in-asp-net/#comments</comments> <pubDate>Wed, 13 Jan 2010 14:15:20 +0000</pubDate> <dc:creator>Gerrod</dc:creator> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[C#]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=249</guid> <description><![CDATA[One thing I&#8217;ve always thought that ASP.NET doesn&#8217;t do a great job of is handling resource paths. For example, lets say on your development box, you have a website hosted under the virtual directory &#8220;MyProject&#8221;, such that you browse to http://localhost/MyProject/ to go to the home page. Lets also say that when you deploy the [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2010/01/14/path-resolution-in-asp-net/"></g:plusone></div><p>One thing I&#8217;ve always thought that ASP.NET doesn&#8217;t do a great job of is handling resource paths. For example, lets say on your development box, you have a website hosted under the virtual directory &#8220;MyProject&#8221;, such that you browse to <span style="font-family:courier">http://localhost/MyProject/</span> to go to the home page. Lets also say that when you deploy the website to an external domain, it falls into the domain root, so instead you go to <span style="font-family:courier">http://myproject.com/</span> to see your home page.</p><p>Sound familiar? The real problem then comes when you try and define a resource (e.g. a javascript file) using a relative path:</p><pre class="brush: xml; title: ; notranslate">
&lt;script
    type=&quot;text/javascript&quot;
    src=&quot;/Scripts/jquery.js&quot;&gt;
</pre><p>This will work fine and dandy when you deploy the website, however on your local machine it will be looking for jquery at the path <span style="font-family:courier">http://localhost/Scripts/jquery.js&#8221;</span> &#8211; which more than likely isn&#8217;t going to work.</p><p>To get around this problem, I wrote a quick helper method called &#8220;Locate&#8221;, which I find myself reusing time and time again between websites. More often than not, I put it into a static class called &#8220;SiteManager&#8221; which basically just contains a bunch of helper functions relevant to the current website. Here&#8217;s the method:</p><pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// Resolves the path to a URL
/// &lt;/summary&gt;
/// &lt;param name=&quot;url&quot;&gt;The path to be resolved, relative to the
/// application root&lt;/param&gt;
/// &lt;param name=&quot;formatArgs&quot;&gt;String formatting arguments&lt;/param&gt;
/// &lt;returns&gt;The URL&lt;/returns&gt;
static public string Locate(string url, params object[] formatArgs)
{
    if (String.IsNullOrEmpty(url))
        url = &quot;/&quot;;

    if (formatArgs != null &amp;&amp; formatArgs.Length &gt; 0)
        url = String.Format(url, formatArgs);

    HttpContext context = HttpContext.Current;
    return String.Concat(
        context.Request.ApplicationPath,
        !url.StartsWith(&quot;/&quot;) ? &quot;/&quot; : String.Empty,
        url);
}
</pre><p>To use the method, you simply change your script declaration (or whatever resource you&#8217;re trying to find) as follows:</p><pre class="brush: csharp; html-script: true; title: ; notranslate">
&lt;script
    type=&quot;text/javascript&quot;
    src=&quot;&lt;%= SiteManager.Locate(&quot;/Scripts/jquery.js&quot;) %&gt;&quot;&gt;
&lt;/script&gt;
</pre><p>You&#8217;ll also notice that it supports string formatting &#8211; so even though it would be pointless in this particular example, you could, if you preferred, do something like this:</p><pre class="brush: csharp; html-script: true; title: ; notranslate">
&lt;script
    type=&quot;text/javascript&quot;
    src=&quot;&lt;%= SiteManager.Locate(&quot;/Scripts/{0}.js&quot;, &quot;jquery&quot;) %&gt;&quot;&gt;
&lt;/script&gt;
</pre><p>In both instances, this will resolve the URL in your output document using the current application path &#8211;</p><ul><li><span style="font-family:courier">http://localhost/MyProject/Scripts/jquery.js</span> on your local machine; and</li><li><span style="font-family:courier">http://myproject.com/Scripts/jquery.js</span> on the web server.</li></ul><p>Enjoy!</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2010/01/14/path-resolution-in-asp-net/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Containing floats in HTML</title><link>http://quickduck.com/blog/2009/11/27/containing-floats-in-html/</link> <comments>http://quickduck.com/blog/2009/11/27/containing-floats-in-html/#comments</comments> <pubDate>Thu, 26 Nov 2009 14:46:30 +0000</pubDate> <dc:creator>Gerrod</dc:creator> <category><![CDATA[Asp.Net]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=219</guid> <description><![CDATA[Have you ever created a block-level element (e.g. a div), whose only contents are floating elements &#8211; only to find that your block-level element doesn&#8217;t stretch down to contain it&#8217;s children? Well, I have, and it&#8217;s a problem that always annoys me because I can never bring to mind the easiest way of fixing it. [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2009/11/27/containing-floats-in-html/"></g:plusone></div><p>Have you ever created a block-level element (e.g. a div), whose only contents are floating elements &#8211; only to find that your block-level element doesn&#8217;t stretch down to contain it&#8217;s children? Well, I have, and it&#8217;s a problem that always annoys me because I can never bring to mind the easiest way of fixing it.</p><p>The simplest solution is to use &#8220;overflow:hidden&#8221; in the style of your parent element, however this doesn&#8217;t work for all browsers scenarios. So, for a much more comprehensive description of the problem, as well as a number of approaches you can use to solve it, check out <a href="http://www.ejeliot.com/blog/59">this post</a>.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2009/11/27/containing-floats-in-html/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>What Is a Race Condition?</title><link>http://quickduck.com/blog/2009/10/06/what-is-a-race-condition/</link> <comments>http://quickduck.com/blog/2009/10/06/what-is-a-race-condition/#comments</comments> <pubDate>Tue, 06 Oct 2009 09:13:44 +0000</pubDate> <dc:creator>Drew Freyling</dc:creator> <category><![CDATA[.Net]]></category> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[Javascript]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=188</guid> <description><![CDATA[Just thought I would put a post out on something I learnt today that can occur when using ajax- a race condition. A race condition is is where the output or result of something is dependent on the timing of other events/code. A good example of this is below: The problem here can occur when [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2009/10/06/what-is-a-race-condition/"></g:plusone></div><p>Just thought I would put a post out on something I learnt today that can occur when using ajax- a race condition. A <a href="http://en.wikipedia.org/wiki/Race_condition" target="_blank">race condition</a> is is where the output or result of something is dependent on the timing of other events/code. A good example of this is below:</p><pre class="brush: jscript; title: ; notranslate">
function ValidateInput() {
//some slow running code here...
}

function SaveForm() {
//save the form
}
</pre><pre class="brush: xml; title: ; notranslate">

&lt;form&gt;
&lt;input type=&quot;text&quot; onchange=&quot;ValidateInput()&quot; /&gt;
&lt;input type=&quot;button&quot; onclick=&quot;SaveForm();&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
</pre><p>The problem here can occur when the user hits the Submit button before the validation method can finish. One way to overcome this is use a <a title="Semaphore" href="http://en.wikipedia.org/wiki/Semaphore_%28programming%29" target="_blank">semaphore</a> to indicate whether the form still needs validating on the SaveForm method.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2009/10/06/what-is-a-race-condition/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>I&#8217;m back</title><link>http://quickduck.com/blog/2008/10/30/im-back/</link> <comments>http://quickduck.com/blog/2008/10/30/im-back/#comments</comments> <pubDate>Thu, 30 Oct 2008 07:06:02 +0000</pubDate> <dc:creator>Ben</dc:creator> <category><![CDATA[.Net]]></category> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[Unit Testing]]></category><guid isPermaLink="false">http://quickduck.com/blog/2008/10/30/im-back/</guid> <description><![CDATA[After having 9 months off from doing .net (travelling, bumming and then a 3 month contract role as an architect in a coldfusion shop) I finally got myself a role doing what I love doing best &#8211; playing with .net and c#. In the last 4 weeks I&#8217;ve been researching and prototyping apps that use [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2008/10/30/im-back/"></g:plusone></div><p>After having 9 months off from doing .net (travelling, bumming and then a 3 month contract role as an architect in a coldfusion shop) I finally got myself a role doing what I love doing best &#8211; playing with .net and c#.</p><p>In the last 4 weeks I&#8217;ve been researching and prototyping apps that use</p><ul><li>.net 3.5</li><li><a href="http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework">ado.net entity framework</a></li><li><a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx">ado.net data services</a> &#8211; including .net, ajax and silverlight client libraries</li><li><a href="http://silverlight.net">Silverlight 2.0</a></li><li><a href="http://msdn.microsoft.com/en-us/library/ms364077(VS.80).aspx">Visual Studio for Software Testers &#8211; Web Testing</a></li></ul><p>It&#8217;s been awesome to have the time and resources to touch across all of these technologies to enjoy their benefits and to loath the limitations.</p><p>All in all I reckon all of the technologies are a great addition to the .net stack and look forward to seeing them mature with each new release.</p><p>I look forward to documenting my findings as I find time to re-work them into don&#8217;t-give-away-IP examples.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2008/10/30/im-back/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Alternating items without using an AlternatingItemTemplate</title><link>http://quickduck.com/blog/2008/08/27/alternating-items-without-using-an-alternatingitemtemplate/</link> <comments>http://quickduck.com/blog/2008/08/27/alternating-items-without-using-an-alternatingitemtemplate/#comments</comments> <pubDate>Wed, 27 Aug 2008 08:06:57 +0000</pubDate> <dc:creator>Gerrod</dc:creator> <category><![CDATA[Asp.Net]]></category><guid isPermaLink="false">http://quickduck.com/blog/2008/08/27/alternating-items-without-using-an-alternatingitemtemplate/</guid> <description><![CDATA[Many of the controls in ASP.Net (e.g. a Repeater) support the concept of an AlternatingItemTemplate, which, as the name suggests, allows you to define an additional item template to change the look and feel for each alternating item in the data source. This is all well and good, but I&#8217;ve never seen a good use [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2008/08/27/alternating-items-without-using-an-alternatingitemtemplate/"></g:plusone></div><p>Many of the controls in ASP.Net (e.g. a Repeater) support the concept of an AlternatingItemTemplate, which, as the name suggests, allows you to define an additional item template to change the look and feel for each alternating item in the data source. This is all well and good, but I&#8217;ve never seen a <i>good</i> use for it; more often than not, all you really want to do in your alternating item is, for example, slightly change the background colour for ease of visual differentiation.</p><p>Though you <i>can</i> use the alternating item template for this, it typically involves you duplicating a lot of code, since the AlternatingItemTemplate is mostly the same as your ItemTemplate (save perhaps for a CSS class declaration somewhere).</p><pre><code>
  &lt;asp:Repeater ID="rpt" runat="server"&gt;
      &lt;HeaderTemplate&gt;
          &lt;table cellpadding="0" cellspacing="0"&gt;
              &lt;tr&gt;
                  &lt;th&gt;First Name&lt;/th&gt;
                  &lt;th&gt;Last Name&lt;/th&gt;
                  &lt;th&gt;Age&lt;/th&gt;
              &lt;/tr&gt;
       &lt;/HeaderTemplate&gt;
       &lt;ItemTemplate&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;%# Eval("FirstName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("LastName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("Age") %&gt;&lt;/td&gt;
              &lt;/tr&gt;
       &lt;/ItemTemplate&gt;
       &lt;AlternatingItemTemplate&gt;
              &lt;tr class="Alternating"&gt;
                  &lt;td&gt;&lt;%# Eval("FirstName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("LastName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("Age") %&gt;&lt;/td&gt;
              &lt;/tr&gt;
       &lt;/AlternatingItemTemplate&gt;
       &lt;FooterTemplate&gt;
          &lt;/table&gt;
       &lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
</code></pre><p>If this sounds like you, then never fear &#8211; <i>there IS a better way</i>!</p><p>When you&#8217;re binding, you have access to the RepeaterItem via the Container property, which contains an ItemIndex property. As such, the simplest way of alternating your items is simply to check if the number divides evenly by two:</p><pre><code>
  &lt;asp:Repeater ID="rpt" runat="server"&gt;
      &lt;HeaderTemplate&gt;
          &lt;table cellpadding="0" cellspacing="0"&gt;
              &lt;tr&gt;
                  &lt;th&gt;First Name&lt;/th&gt;
                  &lt;th&gt;Last Name&lt;/th&gt;
                  &lt;th&gt;Age&lt;/th&gt;
              &lt;/tr&gt;
       &lt;/HeaderTemplate&gt;
       &lt;ItemTemplate&gt;
              &lt;tr class="&lt;%# Container.ItemIndex % 2 == 0 ? "Alternating" : String.Empty %&gt;"&gt;
                  &lt;td&gt;&lt;%# Eval("FirstName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("LastName") %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%# Eval("Age") %&gt;&lt;/td&gt;
              &lt;/tr&gt;
       &lt;/ItemTemplate&gt;
       &lt;FooterTemplate&gt;
          &lt;/table&gt;
       &lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
</code></pre><p>There is a minor downside using this approach &#8211; the non-alternating rows will all declare an empty CSS class within them. But, this is a sacrifice I think is acceptable to decrease repetition of code.</p> ]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2008/08/27/alternating-items-without-using-an-alternatingitemtemplate/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>DataBinding with XPath</title><link>http://quickduck.com/blog/2007/10/12/databinding-with-xpath/</link> <comments>http://quickduck.com/blog/2007/10/12/databinding-with-xpath/#comments</comments> <pubDate>Fri, 12 Oct 2007 12:31:58 +0000</pubDate> <dc:creator>Gerrod</dc:creator> <category><![CDATA[Asp.Net]]></category> <category><![CDATA[C#]]></category><guid isPermaLink="false">http://quickduck.com/blog/?p=44</guid> <description><![CDATA[Did you know that when you bind an item to a DataBound control (e.g. a repeater), you can use the XPath function to evaluate&#8230; well, an XPath? Obviously this is only going to work if you bind an XmlNode (or subclass) instance to the control, but still &#8211; comes in very handy! // in code-behind [...]]]></description> <content:encoded><![CDATA[<div class="google_plus_one"><g:plusone size="standard" count="false" url="http://quickduck.com/blog/2007/10/12/databinding-with-xpath/"></g:plusone></div><p>Did you know that when you bind an item to a DataBound control (e.g. a repeater), you can use the XPath function to evaluate&#8230; well, an XPath? Obviously this is only going to work if you bind an XmlNode (or subclass) instance to the control, but still &#8211; comes in very handy!</p><pre><code>
// in code-behind
rpt.DataSource = document.SelectNodes("/ROOT/students/student");
rpt.DataBind();
</code></pre><pre><code>
&lt;!-- in aspx page --&gt;
&lt;asp:Repeater id="rpt" runat="server"&gt;
    &lt;ItemTemplate&gt;
        &lt;%# XPath("name/text()") %&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:Repeater&gt;
</code></pre>]]></content:encoded> <wfw:commentRss>http://quickduck.com/blog/2007/10/12/databinding-with-xpath/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: quickduck.com @ 2012-02-05 17:01:02 -->
