<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>A.I on the 1s and 0s</title><link>http://weblogs.asp.net/alnurismail/default.aspx</link><description>
Alnur Ismail is a developer for Microsoft</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Recreating Google’s NewsShow using Bing</title><link>http://weblogs.asp.net/alnurismail/archive/2009/10/16/recreating-google-s-newsshow-using-bing.aspx</link><pubDate>Fri, 16 Oct 2009 20:09:33 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7231642</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=7231642</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/10/16/recreating-google-s-newsshow-using-bing.aspx#comments</comments><description>&lt;p&gt;I’ve been looking at and learning the &lt;a href="http://www.bing.com/developers" target="_blank"&gt;Bing 2.0’s API&lt;/a&gt; recently, and comparing it to Google’s API. While browsing Google’s &lt;a href="http://code.google.com/apis/ajaxsearch/" target="_blank"&gt;AJAX Search API&lt;/a&gt; I noticed that Google is focusing on letting their users embed simple apps/controls that take advantage of their search API which provide a lot of value for minimal time invested. I tried to find a similar offering from Bing but came up empty. That’s a pretty smart tactic because if I were an end user (read: not a power user) deciding to leverage an API for my site I would lean heavily toward Google because the barrier to entry is just a few lines of JavaScript. One of Google’s apps that I really liked was the &lt;a href="http://code.google.com/apis/ajaxsearch/documentation/newsshow/" target="_blank"&gt;NewsShow&lt;/a&gt; which lets&amp;#160; users see rotating headlines and previews of Google News Search based on queries that you select. In an attempt to become familiar with the Bing 2.0 API and drink the MS Kool-Aid I decided to recreate that functionality using Bing as the search provider.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/alnurismail/image_04C2CC78.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Google&amp;#39;s NewsShow" border="0" alt="Google&amp;#39;s NewsShow" src="http://weblogs.asp.net/blogs/alnurismail/image_thumb_636326DB.png" width="732" height="94" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;Google’s NewsShow &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/alnurismail/image_2973E3EF.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/alnurismail/image_thumb_6865648A.png" width="665" height="85" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;Alnur’s Some What Ghetto NewsShow Using Bing &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ve copied the code into this blog post since it’s just JavaScript so what you’re, hopefully, seeing below is the actual control running. The code is straight forward: a loop that grabs news head lines for a specific topic, shows the first 4, and then repeats for the next topic.&lt;/p&gt;  &lt;p&gt;&lt;script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script type="text/javascript"&gt;
        // Replace the following string with the AppId you received from the
        // Bing Developer Center.
        var AppId = "AEAD1B8FE5764764B9DC68ADF272342B815E0BB0";
        var _categoryOffset = 0;
        var _articleOffset = 0;
        var _categories = ["", "rt_Business", "rt_Entertainment", "rt_Health", "rt_Political", "rt_Scientific", "rt_Sports", "rt_World", "rt_ScienceAndTechnology"];
        var _categoriesReadable = ["Top Stories", "Business", "Entertainment", "Health", "Political", "Scientific", "Sports", "World", "Technology"];
        var _articles = [];

        $(function() {
            DisplayNews();
        })

        function DisplayNews() {
            if (_articles.length == 0) {
                GetNews();
            }
            else {
                if (_articleOffset &gt;= 4) {
                    _categoryOffset = (_categoryOffset &gt;= _categories.length - 1) ? 0 : _categoryOffset + 1; //circluar
                    _articleOffset = 0;
                    GetNews();
                }
                else {
                    RenderNews();
                }
            }
        }

        //Get the top news stories for each category.
        function GetNews() {
            var requestStr = "http://api.search.live.net/json.aspx?"

            // Common request fields (required)
            + "AppId=" + AppId
            + "&amp;Query="
            + "&amp;Sources=News"
            + "&amp;News.Category=" + _categories[_categoryOffset]
            + "&amp;JsonType=callback"
            + "&amp;JsonCallback=?";

            $.ajax({
                type: "GET",
                url: requestStr,
                dataType: "jsonp",
                success: function(response) {
                    GetNewsComplete(response);
                },
                error: function(response) {
                    alert("Error: " + response.d);
                }
            });
        }

        function GetNewsComplete(response) {
            var errors = response.SearchResponse.Errors;
            if (errors != null) {
                // There are errors in the response. Display error details.
                DisplayErrors(errors);
            }
            else {
                // There were no errors in the response. Redner the
                // News results.
                _articles = response.SearchResponse.News.Results
                RenderNews();
            }
        }

        function RenderNews() {
            if (_articles[_articleOffset] &amp;&amp; _articles[_articleOffset].Snippet) {
                $(".ai-qtitle").html(_categoriesReadable[_categoryOffset]);
                $(".ai-title").html("&lt;a href=\"" + _articles[_articleOffset].Url + " target=\"_parent\" class=\"ai-title-link\"&gt;" + _articles[_articleOffset].Title + "&lt;/a&gt;");
                $(".ai-source").html(_articles[_articleOffset].Source);
                $(".ai-snippet").html(_articles[_articleOffset].Snippet);

                _articleOffset++;
                setTimeout("DisplayNews()", 5000)
            }
            else {
                _articleOffset++;
                DisplayNews();
            }
        }

        function DisplayErrors(errors) {
            //....
        }
    &lt;/script&gt;&lt;/p&gt; &lt;style type="text/css"&gt;


        .ai-root
        {
            font-family: arial, sans-serif;
            font-size: 13px;
            position: relative;
        }
        
        /* Absolute Objects */
        .ai-basecanvas, .ai-poweredby, .ai-poweredbydivider, .ai-result, .ai-query-title
        {
            position: absolute;
        }

        .ai-title a:link, .ai-title a:visited
        {
            color: #000099;
            text-decoration: underline;
        }
        
        /* Common Colors and Sizes */
        .ai-title
        {
            font-weight: bold;
        }

        .ai-snippet
        {
            font-size: 90%;
            color: #000000;
        }
        
        .ai-source
        {
            color: #666666;
        }
        
        .ai-query-title
        {
            color: #000000;
            font-size: 18px;
            font-weight: normal;
        }
        
        .ai-658x80
        {
            width: 658px;
            height: 80px;
            background-color: #ccd9f2;
        }
        
        .ai-658x80 .ai-basecanvas
        {
            top: 1px;
            left: 1px;
            width: 656px;
            height: 78px;
            background-color: #ffffff;
        }
        
        .ai-658x80 .ai-query-title
        {
            width: 163px;
            text-align: center;
            line-height: 1.3em;
            height: 1.3em;
            overflow: hidden;
        }
        
        .ai-generic-658x80 .ai-query-title
        {
            top: 25px;
        }
        
        .ai-658x80 .ai-result
        {
            top: 5px;
            left: 175px;
            width: 455px;
        }
        
        .ai-658x80 .ai-title
        {
            line-height: 1.3em;
            height: 1.3em;
            overflow: hidden;
        }
        
        .ai-658x80 .ai-sourceandtime
        {
            line-height: 1.3em;
            height: 1.3em;
            overflow: hidden;
        }
        
        .ai-658x80 .ai-snippet
        {
            line-height: 1.3em;
            height: 2.6em;
            overflow: hidden;
            margin-bottom: 2px;
        }
    &lt;/style&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="ai-root ai-658x80 ai-generic-658x80"&gt;   &lt;div class="ai-basecanvas"&gt;     &lt;div class="ai-result"&gt;       &lt;div class="ai-title"&gt;&lt;a class="ai-title-link" href="http://www.thestar.com/news/world/article/711237--no-indication-balloon-stunt-a-hoax-colorado-sheriff-says%20target=" _parent?="_parent?"&gt;No indication balloon stunt a hoax, Colorado sheriff says&lt;/a&gt;&lt;/div&gt;        &lt;div class="ai-sourceandtime"&gt;&lt;span class="ai-source"&gt;Toronto Star&lt;/span&gt;&lt;/div&gt;        &lt;div class="ai-snippet"&gt;FORT COLLINS, COLO. – The Larimer County sheriff's department in Colorado says they have no indication a family was carrying out a hoax when they reported their 6-year-old son was in a helium balloon that floated away from their home. The boy was found hiding in the home's garage Thursday ... &lt;/div&gt;     &lt;/div&gt;   &lt;/div&gt;    &lt;div class="ai-query-title"&gt;     &lt;div class="ai-qtitle"&gt;Top Stories&lt;/div&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;The Ghetto NewsShow in full flight&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Feel free to SAM (steal and modify) but if you end up using/improving it let me know! &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7231642" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Bing/default.aspx">Bing</category></item><item><title>Part 2 – Object Oriented JavaScript (MS AJAX)</title><link>http://weblogs.asp.net/alnurismail/archive/2009/06/22/part-2-object-oriented-javascript-ms-ajax.aspx</link><pubDate>Mon, 22 Jun 2009 16:42:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7132089</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=7132089</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/06/22/part-2-object-oriented-javascript-ms-ajax.aspx#comments</comments><description>
&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;I recently gave a presentation on Object Oriented JavaScript (OOJ) and received some good feedback so the next few posts will be a more verbose version of that presentation.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;u&gt;AGENDA&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx" target="_blank" mce_href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx"&gt;Part 1 – JavaScript Review&lt;/a&gt; &lt;br&gt;Part 2 – How &lt;br&gt;Part 3 – Why Use OOJ? &lt;br&gt;Part 4 – Example&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;u&gt;Part 2 – How&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Custom Objects&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Using what was covered in &lt;a href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx" target="_blank" mce_href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx"&gt;Part 1&lt;/a&gt; we can start to create our own custom objects. First we need to write a constructor by using a function:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt; Team(name, league) {&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._name = name;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._league = league;&lt;br&gt;}&lt;/pre&gt;&amp;nbsp; &lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;To create an instance of a Team we can use the function together with the &lt;i&gt;new&lt;/i&gt; operator to create a new object and invoke the constructor:&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; t = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Team(&lt;span style="color: rgb(0, 96, 128);"&gt;'Arsenal'&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;'EPL'&lt;/span&gt;);&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;&lt;u&gt;The Prototype Object&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Briefly mentioned in &lt;a href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx" target="_blank" mce_href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx"&gt;Part 1&lt;/a&gt; the prototype object defines the template of an object. It is similar to a class in C# because it holds all the properties that will be inherited by any instance. However, there is one major difference between an instance of an object in C# that inherits from a class, and an object in JS that inherits from the prototype – &lt;b&gt;all objects and arrays added to the prototype are shared between all instances&lt;/b&gt;. Consequently, it is a best practice to store all the members in the constructor and all methods in the prototype. If you are interested in learning more about the prototype object you can &lt;i&gt;&lt;font color="#ff8000"&gt;[Kool-Aid alert]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;a href="http://www.bing.com/search?q=javascript+prototype+object" target="_blank" mce_href="http://www.bing.com/search?q=javascript+prototype+object"&gt;Bing it&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;The Microsoft AJAX Library&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;If you’re using ASP .NET to build AJAX infused applications you’ve probably heard about and used the &lt;a href="http://www.asp.net/ajax/AjaxControlToolkit/Samples/" target="_blank" mce_href="http://www.asp.net/ajax/AjaxControlToolkit/Samples/"&gt;AJAX Control Toolkit&lt;/a&gt;. Maybe you’ve wondered how these MS guys create such powerful controls? Well, if you take a look at the source you’ll see that they are are using the MS AJAX library to OOJ (&lt;i&gt;it’s a verb and a noun&lt;/i&gt;). So why the hype? Because the MS AJAX library lets you &lt;b&gt;easily&lt;/b&gt; simulate OO constructs such as: classes, properties, interfaces and enumerations that are not currently supported in JavaScript. Let’s take a look at how to use the library…&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Classes&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;To make the above Team object a class all we need to add is &lt;b&gt;one&lt;/b&gt; line of code after the constructor has been declared:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt; Team(name, league) {&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._name = name;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._league = league;&lt;br&gt;}&lt;br&gt;Team.prototype = {&lt;br&gt;    getRoster : &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;() {&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//..&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//code responsible for making the constructor behave as a class&lt;/span&gt;&lt;br&gt;Team.registerClass(&lt;span style="color: rgb(0, 96, 128);"&gt;'Team'&lt;/span&gt;);&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;The registerClass in the above example is responsible for making the constructor behave as a class but it can do more that just that. It is responsible for:&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Registering the type name (ex. Team) in the constructor so you can access it at runtime (normally you declare a class by assigning it to a namespace variable so the class becomes an anonymous function which I talked about in &lt;a href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx" target="_blank" mce_href="http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx"&gt;Part 1&lt;/a&gt;. &lt;b&gt;You always want to use the fully qualified name of the class otherwise you loose the ability to access it&lt;/b&gt;.) &lt;/li&gt;

&lt;li&gt;Resolving an inheritance relationship if a base class is provided &lt;/li&gt;

&lt;li&gt;Letting you specify one or many interfaces &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is an example of how to use registerClass to do those three things :&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;FIFA.League.EPL.Team.registerClass(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.League.EPL.Team'&lt;/span&gt;&lt;br&gt;                                    , FIFA.League.Team&lt;br&gt;                                    , FIFA.League.ITeam);&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;This translate to:&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;FullyQualifiedClassName.registerClass('FullyQualifiedClassName'&lt;br&gt;                                        , BaseClass&lt;br&gt;                                        , Interface);&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;div&gt;&lt;u&gt;Properties&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;To expose properties (aka getters and setters in C#) in a JavaScript object add two functions to the Team prototype object like so:&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;Team.prototype = {&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;//...&lt;/span&gt;&lt;br&gt;    getName() : &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt; () {&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._name;},&lt;br&gt;    setName(): &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt; (value) { &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._name = value;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;By declaring member variables using this pattern there is no way to set private scope so although you could just directly get/set the variables you would loose the ability to perform any required logic. (&lt;i&gt;Note: there is a way to make a variable private in JS using closures but it won’t work following the MS AJAX best practices. I’ll write another post about that because sometimes I’ve found MS AJAX to be overkill when you need a simple standalone object.&lt;/i&gt;)&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Namespaces&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;From the registerClass example above the Team class is being accessed through a series of namespaces. To create a namespace use:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;Type.registerNamespace(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA'&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;For a nested namespace use:&lt;/div&gt;
&lt;div id="codeSnippetWrapper"&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;Type.registerNamespace(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.League'&lt;/span&gt;);&lt;/pre&gt;&lt;br&gt;&lt;u&gt;Inheritance&lt;/u&gt;&lt;/div&gt;
&lt;p&gt;Remember that to accomplish inheritance in JS we need to inherit from the prototype object. We do this by using the registerClass method that was shown above. But, we also need a way to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Call the base constructor &lt;/li&gt;

&lt;li&gt;Pass parameters to the base constructor &lt;/li&gt;

&lt;li&gt;Override methods inherited from the base &lt;/li&gt;

&lt;li&gt;Call base methods &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;MS AJAX makes these four tasks fairly simple. To call the base class constructor and to pass it some parameters use the initializeBase(…) method that takes two parameters. The first parameter is the child class that is inheriting and the second is an array of parameters we need to pass. To override a method replace the function with your own just like in C#, and calling a base method is as easy as calling the callBaseMethod(…) method which takes three parameters: the current instance, the method name to call, and an array of method parameters.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;p&gt;Type.registerNamespace(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA'&lt;/span&gt;);&lt;/p&gt;&lt;br&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//Player Base Class&lt;/span&gt;&lt;br&gt;FIFA.Player = &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt; (name, age) {&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._name = name;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;._age = age;&lt;br&gt;}&lt;br&gt;FIFA.Player.prototype = {&lt;br&gt;    strategy : &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;() {&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//Perform player logic&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;FIFA.Player.registerClass(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.Player'&lt;/span&gt;);&lt;/p&gt;&lt;br&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//Defender Derived Class&lt;/span&gt;&lt;br&gt;FIFA.Defender = &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;(name, age) {&lt;br&gt;    FIFA.Defender.initializeBase(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;, [name, age]);&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;//Perform additional logic &lt;/span&gt;&lt;br&gt;}&lt;br&gt;FIFA.Defender.prototype = {&lt;br&gt;    strategy : &lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;() {&lt;br&gt;        FIFA.Defender.callBaseMethod(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;'strategy'&lt;/span&gt;);&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//Perform additional logic&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;FIFA.Defender.registerClass(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.Defender'&lt;/span&gt;, FIFA.Player);&lt;/p&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Interfaces &amp;amp; Enumerations&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Interfaces and enumerations follow a similar pattern. MS AJAX uses functions to implement both but we need to stop a user from calling the constructor to create instances of them, and also stop a user from calling the methods of the interface. This can be achieved by using Error.notImplemented() in the function body so the actions error out “by design”. We define the functions of the interface or the names of the enumeration in the prototype, and after the declaration call either registerInterface or registerEnum. &lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"&gt;FIFA.Leagues.Teams.ITeam.registerInterface(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.Leagues.Teams.ITeam'&lt;/span&gt;);&lt;br&gt;FIFA.Results.registerEnum(&lt;span style="color: rgb(0, 96, 128);"&gt;'FIFA.Results'&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the next and final part of this series I’ll talk about why you should use OOJ and also provide a comprehensive example that ties everything together.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7132089" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET+AJAX/default.aspx">ASP .NET AJAX</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/MS+AJAX/default.aspx">MS AJAX</category></item><item><title>Part 1 - Object Oriented JavaScript (Using MS AJAX)</title><link>http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx</link><pubDate>Sun, 14 Jun 2009 02:48:16 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7121339</guid><dc:creator>alnurismail</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=7121339</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/06/13/part-1-object-oriented-javascript-using-ms-ajax.aspx#comments</comments><description>&lt;p&gt;I recently gave a presentation on Object Oriented JavaScript (OOJ) and received some good feedback so the next few posts will be a more verbose version of that presentation.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;AGENDA&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Part 1 – JavaScript Review    &lt;br /&gt;Part 2 – How     &lt;br /&gt;Part 3 – Why Use OOJ?     &lt;br /&gt;Part 4 – Example&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;&lt;u&gt;Part 1 – JavaScript Review&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Before we get to the good stuff I want to review the basic fundamentals of JavaScript which we will build on in Part 2.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;JavaScript Objects&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;Believe it or not, JavaScript is a &lt;strong&gt;true&lt;/strong&gt; object oriented language. However, objects in JavaScript are different than objects in C# because JavaScript doesn’t have classes which means you can’t create an instance of a class like you would in C#. Instead you need to manipulate the native Prototype object which can be though of as a template. The Prototype object will be discussed in detail a little later. So what is a JavaScript object? Put simply, a JavaScript object is no more than a collection of name/value pairs. &lt;/p&gt;  &lt;p&gt;You can create a JavaScript object by using:&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; person = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Object();&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;JavaScript Properties&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;JavaScript properties can be added at any time unlike C# where all properties are defined in the class. Properties can be added by using:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;person.name = &lt;span style="color: #006080"&gt;'Alnur Ismail'&lt;/span&gt;;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;Or, to show that JS objects are really no more than a collection of name/value pairs we can use the indexed notation.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;person[&lt;span style="color: #006080"&gt;'gender'&lt;/span&gt;] = male;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;u&gt;JavaScript Functions&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;In JS functions are first class objects. This is a fancy way of saying they can do everything regular objects can do such as being: instantiated, returned by other functions, stored as elements of arrays and assigned to variables. The latter, also referred to as an anonymous function, is important to understand when it comes to OOJ because when a function is assigned to a variable there is no way of getting to that function without knowing the property name. More on this later.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;That’s all you really need to know about JavaScript to take advantage of OOJ. I’ll discuss the “how” of OOJ in Part 2.
  &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7121339" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET+AJAX/default.aspx">ASP .NET AJAX</category></item><item><title>Use ExpressionBuilder To Avoid “Server tags cannot contain &lt;% … %&gt; constructs”</title><link>http://weblogs.asp.net/alnurismail/archive/2009/04/18/use-expressionbuilder-to-avoid-server-tags-cannot-contain-lt-gt-constructs.aspx</link><pubDate>Sat, 18 Apr 2009 19:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7049844</guid><dc:creator>alnurismail</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=7049844</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/04/18/use-expressionbuilder-to-avoid-server-tags-cannot-contain-lt-gt-constructs.aspx#comments</comments><description>&lt;P&gt;The ExpressionBuilder is still unknown to a lot of developers that haven’t experienced the sadistic pleasure of localizing a web application. It was introduce in ASP .NET 2.0 and allows you to bind values to control properties using declarative expressions. &lt;/P&gt;
&lt;P&gt;I learnt about the ExpressionBuilder when I was doing some research on localization best practices in .NET. The recommendation is to use the specialized ResourceExpressionBuilder that creates code to retrieve resource values when the page is executed.&lt;/P&gt;
&lt;P&gt;The ResourceExpressionBuilder is great for localization but what if we want to bind a control’s property to something else? Maybe a value in the Web.config’s AppSetting section. You may have tried something like this:&amp;nbsp; &lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:TextBox&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN class=attr&gt;id&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="foo"&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN class=attr&gt;runat&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN class=attr&gt;text&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="&amp;lt;%=ConfigurationManager.AppSettings["&lt;/SPAN&gt;&lt;SPAN class=attr&gt;FooText&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;"] %&amp;gt;"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Don’t be embarrassed. We’ve all done it at least once, and we’ve all been greeted with: &lt;/P&gt;
&lt;P&gt;&lt;B&gt;Parser Error Message: &lt;/B&gt;Server tags cannot contain &amp;lt;% ... %&amp;gt; constructs.&lt;/P&gt;
&lt;P&gt;Thankfully there is an ExpressionBuilder that can help. the AppSettingsExpressionBuilder provides access to values in the AppSettings section of the Web.config and we use it as follows:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;asp:TextBox&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN class=attr&gt;id&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="foo"&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN class=attr&gt;runat&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="server"&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN class=attr&gt;text&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="&amp;lt;%$ AppSettings: FooText %&amp;gt;"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;The ResourceExpressionBuilder and the AppSettingsExpressionBuilder are both derived from the ExpressionBuilder base class. That means we can create our own but I'll leave that topic for another day.&lt;/P&gt;
&lt;P&gt;Keep in mind that the ExpressionBuilder only works when it is assigned to the property of a control. So you won’t be able to use it, for example, to pass values to a JavaScript constructor. 
&lt;STYLE type=text/css&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;
&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7049844" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category></item><item><title>Maintaining Context in a JavaScript Callback</title><link>http://weblogs.asp.net/alnurismail/archive/2009/04/07/maintaining-context-in-a-javascript-callback.aspx</link><pubDate>Wed, 08 Apr 2009 06:06:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7034912</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=7034912</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/04/07/maintaining-context-in-a-javascript-callback.aspx#comments</comments><description>&lt;P&gt;For reference. To maintain context in a JavaScript callback use a closure and an anonymous function. For example:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;var&lt;/SPAN&gt; curObj = &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;; &lt;SPAN class=rem&gt;//closure to get context in callback&lt;/SPAN&gt;
$(&lt;SPAN class=str&gt;'#foobar'&lt;/SPAN&gt;).animate({ height: 100 }, 1000, &lt;SPAN class=str&gt;"jswing"&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;function&lt;/SPAN&gt;() { 
   alert(curObj.someProperty); 
});&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;To avoid the anonymous function use the Microsoft Ajax helper &lt;EM&gt;Function.createDelegate&lt;/EM&gt; as follows:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;.animate = &lt;SPAN class=kwrd&gt;function&lt;/SPAN&gt;() {
   $(&lt;SPAN class=str&gt;'#foobar'&lt;/SPAN&gt;).animate({ height: 100 }, 1000, &lt;SPAN class=str&gt;"jswing"&lt;/SPAN&gt;, 
      Function.createDelegate(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;.AnimateCallback)
);};

&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;.AnimateCallback = &lt;SPAN class=kwrd&gt;function&lt;/SPAN&gt;() {
   alert(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;.someProperty);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7034912" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET+AJAX/default.aspx">ASP .NET AJAX</category></item><item><title>UI Unit Testing with WatiN - Part #2 – Tips &amp; Tricks</title><link>http://weblogs.asp.net/alnurismail/archive/2009/03/07/ui-unit-testing-with-watin-part-2-tips-amp-tricks.aspx</link><pubDate>Sun, 08 Mar 2009 05:25:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6946199</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6946199</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/03/07/ui-unit-testing-with-watin-part-2-tips-amp-tricks.aspx#comments</comments><description>&lt;P&gt;In my &lt;A href="http://weblogs.asp.net/alnurismail/archive/2008/12/12/ui-unit-testing-with-watin-part-1-introduction.aspx" target=_blank mce_href="http://weblogs.asp.net/alnurismail/archive/2008/12/12/ui-unit-testing-with-watin-part-1-introduction.aspx"&gt;first post&lt;/A&gt; about &lt;A href="http://watin.sourceforge.net/" target=_blank mce_href="http://watin.sourceforge.net/"&gt;WatiN&lt;/A&gt; I introduced the open source API, and provided a simple demo to give a taste of what WatiN is capable off. I ended that post with a promise to write a follow up on some of the lessons I’ve learnt. Here they are:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;.NET control ids are not friendly &lt;BR&gt;&lt;/STRONG&gt;WatiN allows you to select any DOM element based on a specified id (or a myriad of other attributes) as follows: &lt;PRE class=csharpcode&gt;_ie.TextField(&lt;SPAN class=str&gt;"controlId"&lt;/SPAN&gt;)&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Unfortunately, .NET control ids are rarely as friendly.You’ll quickly find your tests littered with control ids like ‘ctl00_ContentPlaceHolder_ControlId’ making the tests hard to read and overly verbose. The solution is to supply your own regular expression when selecting an element to match only the end of the control id as shown below.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; Regex PartialClientId(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; partialClientId)
{
   &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Regex(&lt;SPAN class=str&gt;".*_"&lt;/SPAN&gt; + partialClientId + &lt;SPAN class=str&gt;"$"&lt;/SPAN&gt;);
} &lt;STYLE type=text/css&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;&lt;/PRE&gt;
&lt;P&gt;To use the regular expression you need to provide it as a parameter to the selector:&lt;/P&gt;&lt;PRE class=csharpcode&gt;_ie.TextField(PartialClientId(&lt;SPAN class=str&gt;"controlId"&lt;/SPAN&gt;))&lt;BR&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Tests are linked to ids &lt;BR&gt;&lt;/STRONG&gt;Finding a control by id (or name, class, etc.) creates a dependency on that id not changing. Refactoring a control’s id will break all tests relying on it causing you to make sweeping changes which takes time. The solution I use is to create a static class for each web form in my website, under my test project. The class defines constants for all control ids (or names, classes, etc.) that could be referenced in the unit tests. The unit tests use the constants instead of the actual control ids meaning that any change in the web form only needs to be made in one place. &lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Tests repeat &lt;BR&gt;&lt;/STRONG&gt;If you’re building a suite of tests for your website you’ll quickly see that you have to perform the same actions again and again. For example, if you’re testing your e-store you’ll have unit tests for searching for a product and adding products to the shopping cart. If you code these two tests from scratch, the second test would include searching for products. Again, this means if you make any changes to your search unit test you’ll need to make the same change somewhere else. A simple solution is to think of code re-use. Create helper methods for each action like searching, and then call it where appropriate. For example, my helper method would accept a search string and return the results grid. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;That’s all I’ve got so far. Hope it helps.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6946199" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Web+Test/default.aspx">Web Test</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/WatiN/default.aspx">WatiN</category></item><item><title>Running WCF Ajax-enabled services on Windows Server 2008</title><link>http://weblogs.asp.net/alnurismail/archive/2009/02/04/running-wcf-ajax-enabled-services-on-windows-server-2008.aspx</link><pubDate>Wed, 04 Feb 2009 21:37:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6882928</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6882928</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/02/04/running-wcf-ajax-enabled-services-on-windows-server-2008.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Yesterday I was trying to run an ASP .NET website that leveraged multiple WCF Ajax-enabled services on a Windows Server 2008 box under an IIS7 application but&amp;nbsp;kept running into the following JavaScript error:&amp;nbsp; &lt;EM&gt;"Namespace.Service is undefined". &lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;One of my co-workers had run into this recently and told me the problem was WCF Activation is turned off by default in Windows Server 2008. To turn it on:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Go to Server Manager&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Find the Features Summary&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Click "Add Features"&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Expand the ".NET Framework 3.0 Features" node&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Select "WCF Activation"&amp;nbsp;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Hit "Install"&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P mce_keep="true"&gt;Problem solved.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6882928" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/WCF/default.aspx">WCF</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET+AJAX/default.aspx">ASP .NET AJAX</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Windows+Server+2008/default.aspx">Windows Server 2008</category></item><item><title>A better alternative to using alert() for debugging.</title><link>http://weblogs.asp.net/alnurismail/archive/2009/01/12/a-better-alternative-to-using-alert-for-debugging.aspx</link><pubDate>Mon, 12 Jan 2009 23:02:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6832119</guid><dc:creator>alnurismail</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6832119</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2009/01/12/a-better-alternative-to-using-alert-for-debugging.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;We've all used, and unfortunately some people continue to use, alert() to help debug JavaScript. Thankfully the Microsoft Ajax Library has a Sys.Debug class that exposes methods for logging messages to the browser’s JavaScript console. For reference:&lt;/P&gt;
&lt;P mce_keep="true"&gt;To log a message to the console use: Sys.Debug.trace(“Log this message to the console”);&lt;/P&gt;
&lt;P mce_keep="true"&gt;To dump an object use: Sys.Debug.traceDump(someObject);&lt;/P&gt;
&lt;P mce_keep="true"&gt;If you don’t have a JavaScript console download the Web Development Helper for IE, and Firebug for Firefox, or add the following textarea element to your page:&lt;BR&gt;&amp;lt;textarea id=”TraceConsole” rows=”50” cols=”50”&amp;gt;&amp;lt;/textarea&amp;gt;&lt;BR&gt;This will display all&amp;nbsp;your trace messages&amp;nbsp;in the textarea.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6832119" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/.NET/default.aspx">.NET</category></item><item><title>UI Unit Testing with WatiN - Part #1 - Introduction</title><link>http://weblogs.asp.net/alnurismail/archive/2008/12/12/ui-unit-testing-with-watin-part-1-introduction.aspx</link><pubDate>Sat, 13 Dec 2008 00:04:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6780114</guid><dc:creator>alnurismail</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6780114</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/12/12/ui-unit-testing-with-watin-part-1-introduction.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;There is an arsenal of tools readily available for unit testing web applications. Unit testing both the data access and business logic tiers is a common best practice and a requirement for developing complex web applications. Solutions typically contain hundreds of unit tests exercising every possible use case and edge case covering the data access and business logic tiers but the UI tier is consistently ignored because the tools to create UI unit tests are not robust enough and hard to use.&lt;/P&gt;
&lt;P mce_keep="true"&gt;If you are familiar with VSTS you may be wondering if Web Tests can fit the bill. Although it is possible to exercise the UI using Web Tests they don’t really help you get down to the granularity of being able to assert that clicking a button causes a textbox to reset, and in this crazy world of AJAX and Web 2.0 we need a tool that handles dynamic changes to the DOM without breaking a sweat.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Enter &lt;A class="" title=WatIn href="http://watin.sourceforge.net/" target=_blank mce_href="http://watin.sourceforge.net/"&gt;WatiN&lt;/A&gt;&amp;nbsp;(pronounced as what-in); a fantastic open source API that leverages the browser to conduct web tests instead of emulating it. WatiN integrates easily with the VSTS framework and tests can be coded using C# or VB.NET. Currently WatiN uses IE7 but a &lt;A class="" title="WatIn Downloads" href="http://sourceforge.net/project/showfiles.php?group_id=167632" target=_blank mce_href="http://sourceforge.net/project/showfiles.php?group_id=167632"&gt;CTP&lt;/A&gt;&amp;nbsp;has been released that allows you to test with Firefox as well.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The most impressive feature of WatiN is the level of access you have to the DOM. Web tests are able to find elements by id, index, name, text, CSS class, etc., iterate through collections of elements, and invoke any event.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The following is an example and explanation of how to use WatiN using VSTS. The source code is available at the end of the post.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;U&gt;Environment Setup&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Download WatiN 1.3 &lt;A class="" title="WatIn Download" href="http://sourceforge.net/project/showfiles.php?group_id=167632" target=_blank mce_href="http://sourceforge.net/project/showfiles.php?group_id=167632"&gt;here&lt;/A&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Extract the files and if you are using Vista unblock the assemblies&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;U&gt;Creating the Project&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The downloadable source consists of a C# Test Project that contains a single Unit Test. Be sure to add a reference to the WatiNCore.dll and also copy the Interop.SHDocVw.dll into your /bin. The code I’ll be showing below will differ from the downloadable source to save some space.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;U&gt;The Test&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;I’ll show the test first and then explain what’s going on.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/121208/Fig1.jpg" mce_href="http://weblogs.asp.net/blogs/alnurismail/121208/Fig1.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/121208/Fig1.jpg" border=0 mce_src="http://weblogs.asp.net/blogs/alnurismail/121208/Fig1.jpg"&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 1 – “Hello World” test&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;The test first creates an instance of IE and instructs it to navigate to live.com on a separate process. By creating the test on a separate process the session will not be stored across IE instances. The next instruction, which WatiN will call only after the entire page has loaded, queries the DOM to find a text field with the specified Id and once found enters the text. Next, the DOM is queried again to find a button with the specified name and the click event is triggered.&amp;nbsp; Finally we assert true if the page contains our search text, and our IE object is disposed.&lt;/P&gt;
&lt;P mce_keep="true"&gt;This test is very simplistic and very similar to the example test shown on the WatiN homepage except I’m searching live.com instead of google.com (Yes I like Kool-Aid!).&amp;nbsp; In my next post I’ll cover some more advanced scenarios and some tips and tricks to help make your own WatiN experience a little easier.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Source code &lt;A class="" title="Source Code" href="http://cid-e882c47b39adbc49.skydrive.live.com/browse.aspx/WatiN%7C_Prt1" target=_blank mce_href="http://cid-e882c47b39adbc49.skydrive.live.com/browse.aspx/WatiN%7C_Prt1 "&gt;here&lt;/A&gt;&lt;BR&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6780114" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/WatiN/default.aspx">WatiN</category></item><item><title>Using jQuery with ASP .NET AJAX to create a cascading CheckBoxList</title><link>http://weblogs.asp.net/alnurismail/archive/2008/11/21/a.aspx</link><pubDate>Fri, 21 Nov 2008 21:35:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6749821</guid><dc:creator>alnurismail</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6749821</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/11/21/a.aspx#comments</comments><description>&lt;P&gt;Unless you've been living under a rock you know that Microsoft has recently partnered with jQuery and will be shipping jQuery with future versions of Visual Studio. When the announcement was made I didn't know much about jQuery, how it would work with ASP .NET AJAX, or how it would interface with Visual Studio. I decided to do some research to fill in those gaps and then apply what I learnt by creating a very basic cascading CheckBoxList.&lt;BR&gt;&lt;BR&gt;After I learnt jQuery (I read &lt;A class="" title="jQuery in Action" href="http://www.manning.com/bibeault/" target=_blank mce_href="http://www.manning.com/bibeault/"&gt;jQuery in Action&lt;/A&gt; --&amp;nbsp;and highly recommend it) and set up VS2008; building the app was very natural and required very few lines of code. Sufficed to say I’ve become a fan of jQuery and will be using it going forward for all my JavaScripting.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Environment Setup&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Ok, first things first. The environment needs to be setup to use jQuery and the jQuery IntelliSense.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Install the&amp;nbsp;VS2008&amp;nbsp;patch&amp;nbsp;to enable the jQuery IntelliSense &lt;A class="" title="VS2008 patch" href="http://code.msdn.microsoft.com/KB958502/Release/ProjectReleases.aspx?ReleaseId=1736" target=_blank mce_href="http://code.msdn.microsoft.com/KB958502/Release/ProjectReleases.aspx?ReleaseId=1736"&gt;here&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Download jQuery&amp;nbsp;&lt;A class="" title=jQuery href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.6.js" target=_blank mce_href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.6.js"&gt;here&lt;/A&gt; and the jQuery IntelliSense &lt;A class="" title="jQuery IntelliSense" href="http://docs.jquery.com/Downloading_jQuery#Download_jQuery" target=_blank mce_href="http://docs.jquery.com/Downloading_jQuery#Download_jQuery"&gt;here&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Create a scripts folder under the project and add both the jQuery and jQuery IntelliSense files to it&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Creating the App&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The app consists of two files: a web form that contains the HTML and jQuery code, and an Ajax-enabled WCF web service that returns JSON (JavaScript Object Notation) that will be rendered client side.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig1.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig1.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 1 - Cascading CheckBoxList (Don't judge a book by its cover..I know it looks horrible!)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;U&gt;The Wiring&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;To start, create a new web form and place a ScriptManager on the page. Adding the ScriptManager will register the ASP .NET AJAX library for the page and allow the app to make web service calls. The app also needs to use jQuery so add a new ScriptReference to the ScriptManager and set the path to the jQuery file.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig2.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig2.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 2 - Hooking up jQuery&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;U&gt;The Markup&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;We’ll need two divs, with unique ids (I used chklstSelectLeauge and chklstSelectTeam), to hold the checkboxes. The first div will have two static checkboxes that can be added at design time. The second div’s checkboxes will be added by passing the div to a jQuery function that will load it with the JSON returned from the web service.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig3.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig3.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 3 - Required HTML&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;That’s the entire HTML so it’s time for the fun stuff.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;U&gt;The jQuery&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The benefit of using jQuery is that we’re able to embrace “Unobtrusive JavaScript” which means the behavior is separated from the structure. In order to achieve this paradigm the JavaScript needs to run after all the DOM elements on the page are loaded. Because we are using ASP .NET AJAX we’ll need to use its pageLoad() event instead of jQuery’s ready() event.&lt;/P&gt;
&lt;P&gt;Building the cascading CheckBoxList breaks down into three smaller problems:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Finding the checked league checkboxes&lt;/LI&gt;
&lt;LI&gt;Calling the web service&lt;/LI&gt;
&lt;LI&gt;Creating the team checkboxes &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Let’s tackle these problems one at a time.&lt;/P&gt;
&lt;P&gt;In order to find the checked league checkboxes we need to find all checkboxes under the div, iterate through them to find which ones are checked, and maintain a list of checked values. Well, that’s the traditional JavaScript way of solving the problem. The beauty of jQuery is that we can use a selector to do most of the work for us.&amp;nbsp;Here is&amp;nbsp;the code:&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig4.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig4.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 4 - Selecting checked checkboxes&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The above jQuery selector finds all checked checkboxes that are contained in the specified div. It then applies a function that will run for each item in the set of checkboxes. This function appends, using Sys.StringBuilder which doesn’t suffer from the inefficiencies of string concatenation, the checkbox’s value to our list of values. &lt;/P&gt;
&lt;P&gt;Now the app knows which leagues have been selected but when should we call this function? It makes sense to call it whenever we check or uncheck a league checkbox. jQuery makes that a breeze:&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig5.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig5.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 5 - Adding a click event&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The click event needs to be set as soon as the page finishes loading so the code above goes in the pageLoad() function. &lt;/P&gt;
&lt;P&gt;The next mini-problem is to call the web service. We’ll see the web service later but for now trust me that the following code is all you need to hit a WCF web service:&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig6.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig6.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 6 - Calling&amp;nbsp;the web service&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The code&amp;nbsp;looks&amp;nbsp;at the TeamServices namespace, then looks for a class called TeamService, and then calls the GetTeams method which accepts a list of leagues. The final parameter in the function call&amp;nbsp;tells jQuery that after we make the call to GetTeams, and it is successful, to call the getTeamsComplete function. This is also known as the callback function. The getTeamsComplete function is used to fill the team div with the JSON. It expects the JSON returned from the web service and will then load the data into the div.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig7.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig7.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 7 - The callback function&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The final mini-problem revolves around the loadCheckboxList function that gets called in getTeamsComplete. I’ll show you the code first and explain it after.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig8.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig8.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 8 - Working with JSON&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Looks a lot like the getSelectedLeagues code right? The code first clears the div of any HTML using another function (you can check it out in the attached code at the end) and then starts building up a string, by appending the html needed for a checkbox, for each item in the object. After we have the string we stuff it into the div and we’re good to go. But what is inputData.First and inputData.Second? The answer lies within the web service.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;The Web Service&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;I’m not going to show the entire web service but take my word that it contains a static list (_teams) that contains&amp;nbsp;Triplet objects i.e. (leagueId, teamId, teamName) . The interesting code is the GetTeams method that we are calling from the populateTeams function in our web form. Here it is:&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig9.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig9.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 9 - GetTeams(...)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The method takes a comma separated list of leagues and performs a split resulting in a List of Strings (List&amp;lt;String&amp;gt;). Then for each item (or leagueId) we use LINQ to query&amp;nbsp; _teams for a match and then return a List of Pairs i.e. (teamId, teamName). The web service takes the Pair object and serializes it to JSON so it looks something like {“First”:”foo”, “Second”:”bar”} and this explains why we used those properties in the loadCheckboxList function&lt;/P&gt;
&lt;P&gt;We’re almost done. The final step is to go back to the web form and add a ServiceReference to the ScriptManager so it knows what web service to call.&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/112008/fig10.jpg"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/112008/fig10.jpg" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 10 - Hooking up the web service&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;That’s it! Hopefully this post has helped explain what is required to use jQuery and ASP .NET AJAX in a web app, and has given you some new ideas on how to illuminate the mind and dazzle the eye (bonus if you know where that line is from!).&lt;BR&gt;&lt;BR&gt;Source code &lt;A class="" title=source href="http://cid-e882c47b39adbc49.skydrive.live.com/browse.aspx/Cascading%20CheckBoxList" target=_blank mce_href="http://cid-e882c47b39adbc49.skydrive.live.com/browse.aspx/Cascading%20CheckBoxList"&gt;here&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6749821" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Javascript/default.aspx">Javascript</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/WCF/default.aspx">WCF</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/AJAX/default.aspx">AJAX</category></item><item><title>Microsoft's new careers website</title><link>http://weblogs.asp.net/alnurismail/archive/2008/11/17/microsoft-s-new-careers-website.aspx</link><pubDate>Mon, 17 Nov 2008 18:33:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6744103</guid><dc:creator>alnurismail</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6744103</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/11/17/microsoft-s-new-careers-website.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Microsoft has released a beta version of their new global careers website &lt;A title="Microsoft Careers" target=_blank href="http://careers.microsoft.com/" mce_href="http://careers.microsoft.com"&gt;here&lt;/A&gt;. The beta only shows jobs for Canada, Germany, and GCR but it is a much needed improvement over the current site.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I had a hand in the new site so please check it out and let us know what you think by either using the feedback tool on the site or just post a comment here and I'll pass it to the team.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6744103" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Careers/default.aspx">Careers</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Beta/default.aspx">Beta</category></item><item><title>Abstracting the RegularExpressionValidator’s ValidationExpression</title><link>http://weblogs.asp.net/alnurismail/archive/2008/11/10/abstracting-the-regularexpressionvalidator-s-validationexpression.aspx</link><pubDate>Mon, 10 Nov 2008 21:08:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6728764</guid><dc:creator>alnurismail</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6728764</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/11/10/abstracting-the-regularexpressionvalidator-s-validationexpression.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;The RegularExpressionValidator is one of the most useful validators that come standard with ASP .NET. The RegularExpressionValidator allows you to validate, on both client-side and server-side, whether the value of an associated input control matches the pattern specified in the ValidationExpression property. It isn't uncommon to see a web application littered with almost identical validation markup spread across multiple pages and controls.&lt;br&gt;&lt;br&gt;&lt;a href="http://weblogs.asp.net/blogs/alnurismail/111008/Fig1.jpg" mce_href="http://weblogs.asp.net/blogs/alnurismail/111008/Fig1.jpg"&gt;&lt;img src="http://weblogs.asp.net/blogs/alnurismail/111008/Fig1.jpg" mce_src="http://weblogs.asp.net/blogs/alnurismail/111008/Fig1.jpg" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;i&gt;Figure 1 – Using the built-in validator&lt;/i&gt;&lt;/p&gt;
&lt;p mce_keep="true"&gt;The problem with this approach is code repetition. If an application contained 100 textboxes that only accepted alphanumeric data the code would contain the same ValidationExpression property hard-coded in multiple places. A better solution would be to abstract the ValidationExpression so that if there was a need to alter the regular expression it could be changed once from a central location. &lt;br&gt;&lt;br&gt;This abstraction (factoring out the details) can be achieved by creating a new class that inherits from the RegularExpressionValidator class. By adding a few new properties such as MinLength, MaxLength, and a ValidatonType the code becomes easily reusable for all RegularExpressionValidators.&lt;/p&gt;
&lt;p mce_keep="true"&gt;&lt;a href="http://weblogs.asp.net/blogs/alnurismail/111008/Fig2.jpg" mce_href="http://weblogs.asp.net/blogs/alnurismail/111008/Fig2.jpg"&gt;&lt;img src="http://weblogs.asp.net/blogs/alnurismail/111008/Fig2.jpg" mce_src="http://weblogs.asp.net/blogs/alnurismail/111008/Fig2.jpg" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;i&gt;Figure 2 – Abstracting the regular expression&lt;/i&gt;&lt;/p&gt;&lt;p mce_keep="true"&gt;&lt;br&gt;The GetValidationExpression helper method would get the regular expression (i.e. from the Web.config) by checking the supplied ValidationType (I used an Enum for this). &lt;br&gt;&lt;/p&gt;&lt;p mce_keep="true"&gt;Usage is as follows:&lt;/p&gt;
&lt;p mce_keep="true"&gt;&lt;a href="http://weblogs.asp.net/blogs/alnurismail/111008/Fig3.jpg"&gt;&lt;img src="http://weblogs.asp.net/blogs/alnurismail/111008/Fig3.jpg" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;i&gt;Figure 3 - Usage&lt;/i&gt;&lt;/p&gt;
&lt;p mce_keep="true"&gt;Hopefully this helps illustrate some ideas on keeping code cleaner, leaner and a little more maintainable.&lt;br&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6728764" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Validation/default.aspx">Validation</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category></item><item><title>“Yama-Hama! It's fright night!”  #1</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/31/yama-hama-it-s-fright-night-1.aspx</link><pubDate>Fri, 31 Oct 2008 23:31:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6714591</guid><dc:creator>alnurismail</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6714591</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/10/31/yama-hama-it-s-fright-night-1.aspx#comments</comments><description>&lt;P&gt;Since today is Halloween I thought I’d share a scary &lt;STRIKE&gt;ghost&lt;/STRIKE&gt; bug story that I ran into this week. What better way to get everyone into the Halloween spirit? &lt;/P&gt;
&lt;P&gt;Background:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Running a load test, using one webtest, to gauge how the application handles heavy loads for a very specific scenario&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Problem: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Application is throwing an average of 4 exceptions/sec per user spiking CPU usage&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Story:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Ran load test and confirmed baseline of ~ 4 exceptions/sec per user on average&lt;/LI&gt;
&lt;LI&gt;Reviewed code and saw that Response.Redirect(url) was being used&lt;/LI&gt;
&lt;LI&gt;Replaced Response.Redirect(url) with Response.Redirect(url, false) and ran load test&lt;/LI&gt;
&lt;LI&gt;Saw improvement to ~3 exceptions/sec per user&lt;/LI&gt;
&lt;LI&gt;Added break point in custom error handler, to see the exceptions being caught, and ran load test&lt;/LI&gt;
&lt;LI&gt;Caught a FileNotFound exception with no valuable information&lt;/LI&gt;
&lt;LI&gt;Checked the stack trace and saw “System.Web.StaticFileHandler”&lt;/LI&gt;
&lt;LI&gt;Guessed that there must have been a problem loading a .css&amp;nbsp; or image file which are static&lt;/LI&gt;
&lt;LI&gt;Used ieHttpHeaders and saw a funky GET: GET /~/foo/bar.css&lt;/LI&gt;
&lt;LI&gt;Fixed the style sheet reference and ran load test&lt;/LI&gt;
&lt;LI&gt;Saw improvement to ~2 exceptions/sec per user&lt;/LI&gt;
&lt;LI&gt;Ran load test again&lt;/LI&gt;
&lt;LI&gt;Caught an EventValidation exception &lt;/LI&gt;
&lt;LI&gt;Observed custom error handler throwing a NullReferenceException because Server.GetLastError() was null but there was no check before calling Server.GetLastError.GetBaseException() &lt;/LI&gt;
&lt;LI&gt;Added null check&lt;/LI&gt;
&lt;LI&gt;Ran load test and saw improvement to ~1 exception/sec per user&lt;/LI&gt;
&lt;LI&gt;Disabled EventValidation on pages and ran load test and saw improvement to ~0 exceptions/sec per users&lt;/LI&gt;
&lt;LI&gt;Enabled EventValidation on pages because I didn’t like the solution and began investigating&lt;/LI&gt;
&lt;LI&gt;30mins later –I was baffled&lt;/LI&gt;
&lt;LI&gt;Manually ran the webtest and did not get any EventValidation exceptions&lt;/LI&gt;
&lt;LI&gt;Eureka! moment – checked webtest and saw that the ThinkTime between requests was set to 0 causing the next page request to be called before the first page request had finished loading&lt;/LI&gt;
&lt;LI&gt;Changed ThinkTime between request to 5sec&lt;/LI&gt;
&lt;LI&gt;App now had ~0 exceptions/sec per user&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Ok, so it wasn’t that scary but it was painful! Happy Halloween!&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6714591" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Web+Test/default.aspx">Web Test</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Load+Test/default.aspx">Load Test</category></item><item><title>STOP using Response.Redirect(url)</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/27/stop-using-response-redirect-url.aspx</link><pubDate>Mon, 27 Oct 2008 16:56:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6707308</guid><dc:creator>alnurismail</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6707308</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/10/27/stop-using-response-redirect-url.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;OK, I made the title a little misleading to grab your attention - I'm sneaky like that. Seriously though, there are a few things about Response.Redirect(url) that every ASP .NET developer needs to know.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;Using Response.Redirect() to help with site navigation is standard and all ASP .NET developers use it but, most don’t have time to stop and think about what happens under the hood. Put simply, every call to Response.Redirect(url) will throw an Exception and that’s bad…or is it?&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;Here is a snippet from a w3wp.exe process dump: &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;EM&gt;Exception type: System.Threading.ThreadAbortException&lt;BR&gt;Message: Thread was being aborted.&lt;BR&gt;InnerException: &amp;lt;none&amp;gt;&lt;BR&gt;StackTrace (generated):&lt;BR&gt;SP&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;IP&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Function&lt;BR&gt;000000000643E2F0 0000000000000001 System.Threading.Thread.AbortInternal()&lt;BR&gt;000000000643E2F0 0000098278342F1A System.Threading.Thread.Abort(System.Object)&lt;BR&gt;000000000643E340 00000982BC912E96 System.Web.HttpResponse.End()&lt;/EM&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;This snippet shows that a call to Response.Redirect(url) asks .NET to stop processing the current web page thread and to transfer execution to another thread. Internally a call is made to Response.End() which calls Thread.Abort() which throws an exception.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;Is this good, or bad, or ugly? It’s debatable so I won’t go there. All I'll say is that It would have been nice if there was some other way to notify .NET that a thread needed to be stopped besides throwing an exception but, now that the problem is understood let’s work with it and not against it.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;Work with it by using Response.Redirect()’s overload that accepts a bool to determine if the request should be ended. Calling Response.Redirect(url, false)&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;asks .NET not to abort the thread and as a result the call to Response.End() is skipped and the ThreadAbortException is avoided.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;Is it that simple? Nope. There is a price to pay for not aborting the thread. All the page events and postback events will be processed, and the page will still send its HTML to the browser. Let me say that one more time, &lt;STRONG&gt;all the page events and postback events will be processed&lt;/STRONG&gt;. This means any DB calls or complex calculations will still be performed.&lt;/P&gt;
&lt;P mce_keep="true"&gt;So, what are your choices? Either program defensively by setting Boolean flags to ensure that events are not called by accident or, compromise by limiting the use of Response.Redirect, by ensuring that any call to Response.Redirect(url, false) is wrapped in an if-else block or returns immediately after the call and, most importantly, by analyzing the code to see what impact the call will have.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6707308" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category></item><item><title>ASP .NET - A ValidationSummary with some style</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/16/asp-net-a-validationsummary-with-some-style.aspx</link><pubDate>Thu, 16 Oct 2008 18:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6683204</guid><dc:creator>alnurismail</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alnurismail/rsscomments.aspx?PostID=6683204</wfw:commentRss><comments>http://weblogs.asp.net/alnurismail/archive/2008/10/16/asp-net-a-validationsummary-with-some-style.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Using the built-in ASP&amp;nbsp;.NET validation controls makes validation a breeze. The controls are an&amp;nbsp;easy and powerful way to&amp;nbsp;validate form data client-side and server-side. If&amp;nbsp;you need to display a summarized list of all the validation errors on a page you can use the&amp;nbsp;ValidationSummary control. Unfortunately, as with most out of the box controls, you'll have to sacrifice the ability to style it exactly how you want. &lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig1.png" mce_href="http://weblogs.asp.net/blogs/alnurismail/101408/fig1.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig1.png" border=0 mce_src="http://weblogs.asp.net/blogs/alnurismail/101408/fig1.png"&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 1 - Boring ol' ValidationSummary&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Figure 1 shows&amp;nbsp;the ValidationSummary consists&amp;nbsp;of two parts: the header text, and the error messages. The control does not allow you to specify a custom css class for either of these text elements, nor does it allow you to specify an image. So, how do you create a ValidationSummary with some style? &lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig2.png" mce_href="http://weblogs.asp.net/blogs/alnurismail/101408/fig2.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig2.png" border=0 mce_src="http://weblogs.asp.net/blogs/alnurismail/101408/fig2.png"&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 2 - Stylin' and profilin'&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;A few weeks ago I was asked to create something&amp;nbsp;similar to figure 2.&amp;nbsp;I wanted to avoid creating a custom control because I wasn't&amp;nbsp;adding any functionality.&amp;nbsp;Moreover,&amp;nbsp;creating a custom control from scratch or extending the existing ValidationSummary meant&amp;nbsp;I'd have to&amp;nbsp;code&amp;nbsp;both the client-side and server-side functionality -- I really didn't want to touch &lt;EM&gt;WebUIValidation.js&lt;/EM&gt;. &lt;/P&gt;
&lt;P mce_keep="true"&gt;To start, I&amp;nbsp;played with the header text to see if I could inject some HTML, and I could. &lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig3.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig3.png" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure 3 - Injecting HTML into the HeaderText&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Injecting HTML&amp;nbsp;allowed me to wrap the header text&amp;nbsp;and error messages in span tags and assign a css class as shown above. Next,&amp;nbsp;I needed to add an image to the ValidationSummary.&lt;/P&gt;
&lt;P mce_keep="true"&gt;My first attempt at adding an image to the ValidationSummary was to set the CssClass for the control and&amp;nbsp;use the image&amp;nbsp;as the background-image of the div. I used the following css:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;EM&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig4.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig4.png" border=0&gt;&lt;/A&gt;&lt;BR&gt;Figure 4 - Setting the image as the background&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;It&amp;nbsp;worked! But,&amp;nbsp;when I&amp;nbsp;tested different display modes and edge cases I found, in certain situations, the error messages would not align correctly.&amp;nbsp;I couldn't&amp;nbsp;fix this because I had no way of&amp;nbsp;wrapping all the error messages in a div. I also tried&amp;nbsp;adding the image using HTML in the header text but ran into a similar problem. &lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig5.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig5.png" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure&amp;nbsp;5 - Alignment problem&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;To fix the alignment problem I created a two column layout. I placed the image in the left column&amp;nbsp;and the ValidationSummary in the right column.&amp;nbsp;Next I whipped up some Javascript&amp;nbsp;to tie the image and ValidationSummary together so they would show/hide in unison.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig6.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig6.png" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure&amp;nbsp;6 - Two column layout&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://weblogs.asp.net/blogs/alnurismail/101408/fig7.png"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alnurismail/101408/fig7.png" border=0&gt;&lt;/A&gt;&lt;BR&gt;&lt;EM&gt;Figure&amp;nbsp;7 - Javascript to show/hide image&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;I&amp;nbsp;used the client-side method Page_ClientValidate which let me test if&amp;nbsp;my controls&amp;nbsp;met the validation criteria. In this case&amp;nbsp;I also passed&amp;nbsp;a&amp;nbsp;validation group so it didn't interfere with any other validation on the page.&amp;nbsp;I hooked up my button's OnClientClick to call the&amp;nbsp;Javascript and there it was...a ValidationSummary with some style.&lt;/P&gt;
&lt;P mce_keep="true"&gt;One word of caution, since the image is only tied to the ValidationSummary using Javascript you'll need to write some server-side code to show/hide the image in case the user has disabled Javascript.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6683204" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alnurismail/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/Validation/default.aspx">Validation</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ASP+.NET/default.aspx">ASP .NET</category><category domain="http://weblogs.asp.net/alnurismail/archive/tags/ValidationSummary/default.aspx">ValidationSummary</category></item></channel></rss>