Subscribe: Infinities Loop
http://weblogs.asp.net/infinitiesloop/rss.aspx
Added By: Feedage Forager Feedage Grade B rated
Language:
Tags:
add  ajax  control  data  event  it’s  javascript  net ajax  net  new  page  script  server  side  visual studio   
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Infinities Loop

Infinities Loop



ASP.NET and .NET from a new perspective



 



Minecrafter: The Minecraft Companion App for Windows Phone 7

Mon, 08 Nov 2010 16:43:00 GMT

Introducing Minecrafter! Minecrafter is an interactive guide for Minecraft. Every item, block, crafting pattern, and monster is described in detail in a beautiful, searchable interface. It’s also designed to help you find what you need faster. It’s not just a giant list of everything. For example, every item detail page shows you not only how to craft or smelt that item, but items it can be used to craft or smelt. Every icon displayed can also be clicked on to navigate to that item, allowing you to quickly jump to related information. Minecrafter also comes with game guides to help you learn about some of the finer details of the game, such as how water behaves, and tips on how to place blocks efficiently. The guide list is currently small, but I will be adding to them all the time. Minecraft If you don't know, Minecraft is an up-and-coming adventure slash RPG slash strategy Java game. It is unique in that it's graphical style is almost comically simple and low-res. It's kind of like Q-Bert meets DigDug meets World of Warcraft. Yes, that's the perfect way to describe it :) But don't let the graphics fool you. Minecraft is incredibly deep, addicting, fun, and its cup runeth over with potential. Visit the website to play a free version in-browser, or purchase the Alpha game at a discounted price. I highly recommend the latter -- the in-browser version does not do it justice, and you can't play multi-player, which offers a whole new dimension of fun (although is currently wrought with missing features that you get in single player mode, like the ability to DIE, although that does have advantages!). Screen Shots [...]



StackUnderflow: A JavaScript Library and Mashup Tool for StackExchange

Mon, 07 Jun 2010 21:26:00 GMT

StackUnderflow is a JavaScript library that lets you retrieve – and render – questions from the StackExchange API directly on your website just by including a simple, lightweight script. The library is fully documented, so for technical details please check out the StackApps entry for it, and follow the links to the GitHub repository. The rest of this post is about my motivation for the library, how I am using it on the blog, and some other thoughts about the API. StackExchange (e.g. StackOverflow) has recently published an API (still in beta). It’s not very often that such a rich source of data suddenly becomes accessible. So it got me a little excited about all the possibilities. I think the full set of possibilities has yet to be realized, even by the rapidly growing set of entries in the StackExchange API Contest. Like most new things, it takes time. Plus, the API is currently read-only, but it seems they have plans to add write support in the future. Now that will be interesting. An idea for utilizing that in a novel way just popped into my head, just now. Anyway – one thing I have noticed over the last few years is just how much StackOverflow.com has grown as a referrer to this blog. There’s an untold number of SO questions thank here as a reference. StackOverflow is consistently one of my top referrers. I joked once that my SO rep is grossly understated, for credit for all those answers, I get not. Searching for “InfinitiesLoop” on StackOverflow returns over 100 results: http://stackoverflow.com/search?q=infinitiesloop So the first thought I had for utilizing the API is to bring those questions directly to this blog. I want readers to be able to see the SO questions that link to my blog in general, or to each individual blog entry. The nature of my blog entries are such that most of my referrers are from Google searches, people looking for answers to problems they have. It’s only natural to try and bring two sources that are very likely to help together, is it not? It’s a perfect marriage if you ask me! Searching with the StackExchange API As always seems to be the case, as soon as I start dipping my feet into some new technology, I immediately discover that what I want to accomplish is beyond its limitations. Fooey. The StackExchange API does not support searching the body or answers of a question, only the tags and the title. The reason stated is for performance – they suggest you use a proper search engine to look for questions by their content instead. That makes sense, I guess. Why reinvent the wheel, Google and the like are more than capable. The way you do it is pretty simple. All StackExchange questions are found under the “/questions” url, and you can restrict matches to that url. Here’s a Google search that finds all questions linking here: http://www.google.com/search?hl=en&q=site:stackoverflow.com/questions+weblogs.asp.net/infinitiesloop You’ll see in the results that all the urls look like this: http://stackoverflow.com/questions/questionid/title Ah ha – so doing this I can get the question IDs! Abstracting it Away So, the first thing I did was work around this limitation by utilizing an AJAX Google search to find the questions, then the StackExchange API to retrieve the questions. There’s two disadvantages to that: (1) The Google API I’m using is one that limits results to 8 per request, and (2) We must now perform at least two requests to get the data. But I think these disadvantages are no biggie – it wouldn’t be common to find more than 8 questions for one article, and even if there were, the top 8 results should be very relevant, and I don’t necessarily want to bombard you with every result anyway. And the added delay is no biggie – it’s still very fast, and this content is intended to be shown as a sort of ‘extra’ part of the site, which will silently load while the user is focusing on the main content. Rendering the Questions/Data Of course I fully anticipate there to be a very rich JavaScript A[...]



VSNewFile: A Visual Studio Addin to More Easily Add New Items to a Project

Tue, 18 May 2010 20:30:00 GMT

My first Visual Studio Add-in! Creating add-ins is pretty simple, once you get used to the CommandBar model it is using, which is apparently a general Office suite extensibility mechanism. Anyway, let me first explain my motivation for this. It started out as an academic exercise, as I have always wanted to dip my feet in a little VS extensibility. But I thought of a legitimate need for an add-in, at least in my personal experience, so it took on new life. But I figured I can’t be the only one who has felt this way, so I decided to publish the add-in, and host it on GitHub (VSNewFile on GitHub) hoping to spur contributions. Adding Files the Built-in Way Here’s the problem I wanted to solve. You’re working on a project, and it’s time to add a new file to the project. Whatever it is – a class, script, html page, aspx page, or what-have-you, you go through a menu or keyboard shortcut to get to the “Add New Item” dialog. Typically, you do it by right-clicking the location where you want the file (the project or a folder of it): This brings up a dialog that contains, well, every conceivable type of item you might want to add. It’s all the available item templates, which can result in anywhere from a ton to a veritable sea of choices. To be fair, this dialog has been revamped in Visual Studio 2010, which organizes it a little better than Visual Studio 2008, and adds a search box. It also loads noticeably faster.   To me, this dialog is just getting in my way. If I want to add a JavaScript script to my project, I don’t want to have to hunt for the script template item in this dialog. Yes, it is categorized, and yes, it now has a search box. But still, all this UI to swim through when all I need is a new file in the project. I will name it. I will provide the content, I don’t even need a ‘template’. VS kind of realizes this. In the add menu in a class library project, for example, there is a “Add Class…” choice. But all this really does is select that project item from the dialog by default. You still must wait for the dialog, see it, and type in a name for the file. How is that really any different than hitting F2 on an existing item? It isn’t. Adding Files the Hack Way What I often find myself doing, just to avoid going through this dialog, is to copy and paste an existing file, rename it, then “CTRL-A, DEL” the content. In a few short keystrokes I’ve got my new file. Even if the original file wasn’t the right type, it doesn’t matter – I will rename it anyway, including the extension. It works well enough if the place I am adding the file to doesn’t have much in it already. But if there are a lot of files at that level, it sucks, because the new file will have the name “Copy of xyz”, causing it to be moved into the ‘C’ section of the alphabetically sorted items, which might be far, far away from the original file (and so I tend to try and copy a file that starts with ‘C’ *evil grin*). Using ‘Export Template’ To be completely fair I should at least mention this feature. I’m not even sure if this is new in VS 2010 or not (I think so). But it allows you to export a project item or items, including potential project references required by it. Then it becomes a new item in the available ‘installed templates’. No doubt this is useful to help bootstrap new projects. But that still requires you to go through the ‘New Item’ dialog. Adding Files with VSNewFile So hopefully I have sufficiently defined the problem and got a few of you to think, “Yeah, me too!”… What VSNewFile does is let you skip the dialog entirely by adding project items directly to the context menu. But it does a bit more than that, so do read on. For example, to add a new class, you can right-click the location and pick that option. A new .cs file is instantly added to the project, and the new item is selected and put into the ‘rename’ mode immediately. The default items available are shown here. But you c[...]



Tokenizing JavaScript - A look at what’s left after minification

Wed, 21 Apr 2010 18:53:00 GMT

Minifiers JavaScript minifiers are popular these days. Closure, YUI Compressor, Microsoft Ajax Minifier, to name a few. Using one is essential for any site that uses more than a little script and cares about performance. Each tool of course has advantages and disadvantages. But they all do a pretty good job. The results vary only slightly in the grand scheme of things. Not enough to make so much of a difference that I’d say you should always use one over the other – use whatever fits in with your environment best. Tag Clouds Anyway, it got me thinking. After crunching a script through one of these bad boys, what’s left? The first thing I did was take jQuery 1.4.2 (the minified version) and push it into the tag cloud creator, Wordle. BAM! Beautiful, isn’t it? It’s not that surprising that two of the longest keywords in JavaScript also happen to use up the most space: return, and function. What does it matter? The word function appears in jQuery 404 times, adding up to 3,232 bytes. That’s about 4.5% of the size of the library! return appears 385 times, adding up to 2,310 bytes, or 3.2%. So there you go – return and function make up a total of almost 8% of the size of jQuery! Really makes me wish JavaScript had C# style llamas – err, lambdas. Tokenizing There are some problems here though. No tag cloud generator I could find was intended to be run on code. So it ignores things like operators, brackets, etc. And you know things like semicolons are frequent. Nor do they provide any kind of data feed of the document’s tokens. So, I created my own tool to generate the data. Basically, I just have a list of possible tokens, and I run a regular expression on the code to determine how many times each occurs. Then, multiply by its length to get the total size of that token in the script. Results It’s amazing what the results show. The top 15 tokens represent 35% of the entire script, mostly single-character tokens: It makes sense that function is the top token in size since 8 characters long, even though it only occurs 404 times. But look at “.”. Yes, dot. It’s only one character long, but it represents 2,565 bytes. “(” and “)” together make up over 5k. return, despite being 6 characters long, is in 5th place. What does it mean? Well, actually, the fact that these syntax related tokens are so high on the list is partially a testament to the effectiveness of minifiers. A minifier can only remove so much of the syntax, and you can’t shorten them. So if a minifier is doing it’s job, they should tend bubble up to the top of the list. Thankfully, Wordle supports an advanced mode that lets you enter the tokens and their weight manually. Armed with the output of this tool, here is the entire result set in tag cloud form. The relative sizes of the tags aren’t really correct though, simply because a “.” is smaller in whatever font than any letters. Also, I don’t really know why the first use of Wordle produced a cloud that shows return bigger than function – I guess just a bug in how it counts. All the more reason to use the advanced mode. One thing it does is show ways that minifiers could do an even better job, or ways that we can code that reduce these tokens. For example, in theory a minifier could convert functions that use ‘return’ to assign to a parent-scope variable instead. That’s a fairly complex thing to do, so probably not worth it (performance seems equivalent, though). You can also try and structure your code so a function only has one ‘return’ instead of multiple. This tool can help you find tokens in your code that use a lot of space besides these, too. For example, I applied this tool to the MicrosoftAjax script from .NET 3.5, and found to my horror that ‘JavaScriptSerializer’ was near the top of the list. And that is why in the AjaxControlToolkit you will find this script has been greatly reduced in size. Despite having many new features, it’s 10k sm[...]



Join the Dark Side of Visual Studio 2010

Tue, 20 Apr 2010 20:38:00 GMT

Hard to believe it’s been so long, but it was almost 4 years ago when I published Join the Dark Side of Visual Studio. That was when a lot of people were still using VS2003, and importing and exporting environment settings required a custom add-in, VSStyler, which has since fallen off the planet and is hard to find (link, anyone? Let me know). Three versions of VS later, and I’m still using and loving the dark side. Pleased, I am (haha). In fact, that article for one reason or another is still one of my most popular blog entries, thanks in part to a link from Scott Hanselman and a commenter on Coding Horror. I will point out selfishly that my article predates both of these :) But, yes, it’s sad when one of your top referrers is from a link in a comment on another blog. Not even the first comment, either.

That article even inspired someone out there to register a new domain: http://darksideofvisualstudio.net/

Now that Visual Studio 2010 is out, I decided to repost with my latest settings, exported from Visual Studio 2010. So here you go. It’s mostly the same as the original, but with some improvements. I lightened up some of the blues which are hard to read on some not-so-great monitors. Set the font to Consolas (though you may prefer Inconsolata). Also fixed a few neglected settings like the refactoring view and XML.

I should also point out that unfortunately, even in the new WPF based VS2010, you are very limited in what colors you can change in the environment itself, such as the Solution Explorer. However, there is a very nice add-in from the Visual Studio Platform team that lets you theme many aspects of the IDE that you can’t normally change. Not all, unfortunately, since not everything in VS is WPF yet. But if you use this in combination with the theme settings in windows itself, you can get pretty close to an all-dark theme in all windows. If anyone out there has success with this, please do share your exported theme settings from this add-in as well as your windows theme settings, I will be sure and update this article with the details.

Here’s a quick preview of some code from the AjaxControlToolkit:

(image)

Download The Dark Side of Visual Studio 2010

For older versions, see the previous article.

UPDATE: Rate the scheme on StudioStyles.info! The Dark Side of Visual Studio




ASP.NET 4.0 ScriptManager Improvements

Tue, 24 Nov 2009 05:51:00 GMT

.NET Framework 4 Beta 2 has been out for a little while now. There are some subtle improvements to the ScriptManager control in ASP.NET 4.0 that few have picked up on yet. Allow me to introduce you to them! First, let me say that this is strictly about features in the server-side ASP.NET ScriptManager control, not the Ajax library in general. Also – if you do not use the ASP.NET Ajax library but you are a WebForms developer, I assume you, this article is still for you! EnableCdn? Yes please. This one has been blogged about by ScottGu already, but for completeness, here it is. The ASP.NET Ajax scripts are now hosted in a Microsoft CDN, and you can tell ScriptManager to load them from there by simply enabling this property. For virtually no work you get better performance, less bandwidth usage, and a cleaner rendering due to those ScriptResource.axd urls going away from your HTML. Do read the linked post for details. But wait, there’s more! What has not really been blogged about is this: The EnableCdn property isn’t only for ASP.NET Ajax scripts. Even the scripts in System.Web.dll are on the CDN. So if you are using a GridView, TreeView, or Validators, for example – those scripts will load from the CDN, too. It wouldn’t have been the best experience if only only some scripts were affected by this setting, right? But what if you are using custom scripts, or 3rd party controls? How would that work? Here’s how. Normally, when you embed a script within an assembly for use by a WebForm (either via ScriptManager or the classic GetWebResourceUrl and RegisterScriptResource APIs), you have to define a WebResourceAttribute for it, which allows it to be accessed via the WebResource.axd or ScriptResource.axd handlers: [assembly: WebResource("Foo", "application/x-javascript")] Now, there’s a new property on WebResourceAttribute: CdnPath. Don’t get too caught up on the fact it’s a hard coded url. More on that later. [assembly: WebResource("Foo", "application/x-javascript", CdnPath = "http://foo.com/foo/bar/foo")] ScriptManager looks for this property when the EnableCdn property is set to true, and simply uses that path instead of the ScriptResource.axd path to the assembly resource. Pretty simple. And this means that you too can provide your own CDN paths for your own assembly resource scripts. As for where to host your script, well, that’s up to you.   AjaxFrameworkMode.Disabled ScriptManager does some interesting and useful things, like: script combining, serving scripts from assemblies, script localization, script globalization, removing of duplicate references, automatic switching between debug and release scripts, and now automatic switching to a CDN. Trouble is, it comes with a bit of a tax: It always includes the Microsoft Ajax library (MicrosoftAjax), and by default, MicrosoftAjaxWebForms (for partial rendering support with UpdatePanel). The partial rendering script could be removed by setting EnablePartialRendering=false, but there was no way to disable MicrosoftAjax. So if you wanted to use the useful features of ScriptManager without using MicrosoftAjax, well, you couldn’t (although Bertrand once blogged a way of hacking it by making use of it’s de-duping process). The AjaxFrameworkMode property adds two new modes to ScriptManager’s behavior. Enabled (the default, and the same behavior as before), Explicit (does not include any scripts by default but still assumes Microsoft Ajax is to be utilized), and Disabled (does not include any scripts by default and does not assume Microsoft Ajax will be used). So here I have a ScriptManager that is including a custom script, without also loading MicrosoftAjax or any of the inline script it normally produces.               &n[...]



Microsoft Ajax 4 Preview 5: The DataView Control

Fri, 11 Sep 2009 20:00:00 GMT

Preview 5 of the Microsoft Ajax 4.0 library has been released. Some quick background – this the next version of the client-side ajax framework you have probably already heard of, the one that ships along with ASP.NET 3.5 (but is also available in script form). The fact it ships with ASP.NET has sometimes led to it being called the ASP.NET AJAX Framework. Technically, that name is a superset – it encompasses “Microsoft Ajax” and it’s ASP.NET specific, server-side features, like the ScriptManager. But “Microsoft Ajax” always has and always will be an ajax framework that is not tied to any specific server-side technology. Also noteworthy is that this 4.0 library can run on top of ASP.NET 3.5, replacing the 3.5 version it normally uses. In previous previews of Microsoft Ajax 4, we introduced the DataView control. It’s a simple but powerful control that takes advantage of the client templating engine we built for it. You point it at a template, and some data, and it instantiates the template for each item in the data. The templating engine lets you use ‘placeholders’ and create ‘live bindings’ between the rendered DOM elements and the data, as well as allow you to attach other ajax components and controls. I want to focus on one of the new features added to the DataView in preview 5, but first, I feel obliged to introduce the DataView control from a basic level, since most of you probably don’t know about it yet. Simple DataView Example As a simple example – lets make up some data. Normally this would come from a web service or some other source, not hard coded, of course. var stockData = [    { symbol: "DJIA"  , change: 79.88, value: 9627.48 },    { symbol: "NASDAQ", change: 23.63, value: 2084.02 },    { symbol: "AAAA"  , change: -0.22, value: 27.56 },    { symbol: "BBBB"  , change: -1.46, value: 82.12 },    { symbol: "CCCC"  , change: 0.67 , value: 7.56 }]; We want to show this stock data in a table, and probably do some formatting to show the values as currency, etc. Without a templating engine, you’d basically either have to painstakingly create DOM elements by hand, or build up a string and use innerHTML. Neither technique is easy to maintain, or are designable by designer tools. Worse, building a string and using innerHTML could net you some XSS vulnerabilities. This is where the templating engine in Microsoft Ajax 4 comes in.                                        
{{ symbol }}{{ value.localeFormat("c") }}{{ change.localeFormat("c") }}{{ (change/value).format("p2") }}
In the simple form, {{ foo }} represents a placeholder where the value of a field named ‘foo’ from the data items will be. What language is this stuff? Just javascript. So as you can see, this take advantage of the formatting methods in Microsoft Ajax to convert the values to currency. You could write any ole javascript here, of course (unobtrusive javascript alert: yes, you can do it that way too, more on that later). The class “sys-template” is setup to have display:none, so that this chunk of HTML doesn’t actually show up in the markup. That’s the template – it represents what each item should look like. Now to put it to use with the DataView control: function pageLoad() {    $create(Sys.UI.DataView, {        data: stockData    }, null, null, $get("stocks"));} That’s it. The DataView control is given the data, and the template. Everything is hooked up. H[...]



ASP.NET WebForms: Taking Back the HTML

Wed, 01 Jul 2009 23:44:00 GMT

There’s a lot of debate these days about the ASP.NET WebForms model vs. the newer ASP.NET MVC model. There are advantages to both. Disadvantages to both. Pick the one that best fits your needs. Nuff said. But sometimes that choice isn’t so obvious. MVC for example essentially gives you much more control over the generated HTML for the page. Well, complete control. But sometimes you don’t really need complete control, and the lack of an encapsulated control kind of sucks. HTML Helpers help, but they have no design time experience. Hence, there are the MVC Controls in the MVC Futures project on CodePlex and an interesting compromise between the two models, at least when it comes to the rendering part of the application. But then there’s the other side of the equation – you’ve got a WebForms application, and you need a little more control over the HTML. What’s the compromise there? Well, server controls try to give you control over the markup. Some of them do a pretty good job at it, like the ListView control added in ASP.NET 3.5. Others, not so good. If a control doesn’t let you do what you need, you are pretty much stuck – either abandon the control and all its usefulness, or write your own control. What’s in a control anyway? If you think about it, controls at the highest level are really two very different things. Controls abstract the rendering of HTML. Controls provide client-server integration by managing data between client and server. Take the CheckBox control. It renders an input and label, and connects them with the ‘for’ attribute, so clicking the text also checks and unchecks the checkbox. That’s it’s HTML abstraction. It provides client-server integration by allowing you to dynamically read and write to the checked property on a server-side proxy. A simple example of #2 is the TextBox control. Double meaning, uh, not intended. Type some text into the box and submit the form – magically, the server-side instance knows what its Text property should do. The control can also push data back to the client. Set the Text property, and magically the value is reflected in the rendered page. This can be much more complex though – the control might manage a hidden field, or rely on data stored in that catch-all hidden field we love to hate, ViewState. The thing is, HTML abstraction is useful, but a lot of the time it doesn’t provide a ton of benefit. Client-server integration, however, is usually much more useful. If you don’t need that there’s a good chance you don’t need a control to begin with. Html Controls I would be wrong not to point out that there’s a whole namespace of controls in ASP.NET that are largely underused. HTML controls allow you to add a runat=”server” to an HTML element and get reasonable client-server integration with it. So if you don’t like how the CheckBox control renders, just use the HtmlCheckBox instead. But what if you’re using a more complex control that has no HTML equivalent? What if you could control the HTML for any server control without losing the client-server interaction? Wouldn’t that be nice? Taking Control Introducing the CustomRender control. This control suppresses the rendering of any control(s) you put in it, while allowing you to define your own, replacement HTML. It gets a little nicer than that, though. One step at a time – first, lets choose the enemy. A Button control that sets the text of a Label control. You’re going down, Button1. Bring on the CustomRender:



Professional ASP.NET 3.5 AJAX Released

Tue, 10 Feb 2009 23:35:00 GMT

Looking for an ASP.NET AJAX book? This one has been massively updated from its 2.0 version, to cover in detail the features added in 3.5, and not to be forgotten, 3.5 SP1. For example, Script Combining was a new feature in 3.5 SP1, and so was History support, so it's an important distinction! The 2.0 book was 307 pages, and this one is 552. History support, by the way, has a dedicated chapter. Script combining is a major portion of the chapter on the ScriptManager.

 Another important difference between the 2.0 and 3.5 versions -- this time, I'm a contributing author. :) My first major technical publication, hopefully more to come. I'm that weird guy on the right.

(image)

http://www.amazon.com/Professional-ASP-NET-AJAX-Bill-Evjen/dp/0470392177




The Event Handler That Cried Wolf

Wed, 14 Jan 2009 16:00:00 GMT

I ran into an interesting and unexpected behavior in ASP.NET AJAX event handling the other day. Once I figured out what was going on, I was almost positive it was a bug, so I started looking into what a solution would be. But just out of curiosity, I looked at what the behavior was for a server-side event. Much to my surprise, the behavior was the same. The behavior then was consistent with the server-side behavior, not a bug. But is it the "correct" behavior? Tell me what you think... To put it in the most shocking way possible: Removing a handler from an event does not guarantee that handler will not be called when the event fires! SACREBLEU!! But it's true. Take a look at this code... public class Bar {     public event EventHandler SomeEvent;       public void RaiseEvent() {         if (SomeEvent != null) {             SomeEvent(this, null);         }     } }   public class Foo {     private Bar _bar;     private EventHandler _handler;     private ArrayList _list;       public void Initialize(Bar bar) {         _bar = bar;         _list = new ArrayList();         // listen to the event on bar         _handler = new EventHandler(OnSomeEvent);         _bar.SomeEvent += _handler;     }       private void OnSomeEvent(object sender, EventArgs args) {         // event was raised, do something         // this cant happen if Stop() was called.... right?         _list.Add("blah");         Console.WriteLine("OnSomeEvent");     }       public void Stop() {         // tidy up, stop listening to the event and clear the list         _bar.SomeEvent -= _handler;         _list = null;     } } Now, this is a totally contrived example, so the code here isn't exactly useful. But the basic idea can occur in a real program. Here, there's a Bar component that exposes an event, SomeEvent. Normally a component raises it's event when appropriate and doesn't have an actual RaiseEvent() method to raise it, but for demo purposes, it's got one so we can fire the event later on. The Foo component has two public methods -- Initialize, and Stop. Initialize takes a Bar component, and it adds an event listener to the event. Stop removes the event listener, since after all, it is important to clean up after yourself when it comes to events. If the handler was never removed, the Bar component will forever have a reference to the Foo, and Foo will live as long as Bar lives. When the SomeEvent fires, Foo adds to an ArrayList which it has internally created. Notice in Stop() it sets the ArrayList to null. The event handler OnSomeEvent adds to the list without checking it for null. If somehow the event handler were called even after Stop(), we'd have a null reference exception! Can't happen, right? When you think about a typical use of these components, it doesn't appear to be possible... Here would be one way of raising the event and removing the handler. public class Program {     public static void Main(string[][...]



ASP.NET AJAX 4.0: Observing updates to POJOs (Plain 'Ole JavaScript Objects)

Mon, 10 Nov 2008 07:27:00 GMT

First of all, if you haven't already done so, download the ASP.NET AJAX 4.0 Preview 3 now and try it out! And after you're done with that, come back here, or there, or there, or there, and let us know what you think and what you like or don't like about it. We love feedback, and it really does have an impact on the product. We wouldn't go through all the trouble of releasing preview bits or presenting these things if it didn't. So, I thought I would detail one of the interesting yet behind-the-scenes features in the 4.0 preview, because on its own it is pretty useful. I say behind-the-scenes, because it is used by and invented for many of the features in the preview, such as Bindings and the DataView control, but it is also a public API that you can use for your own purposes. A bit about JavaScript Objects Objects in JavaScript are associative arrays. If you are familiar with the .NET Hashtable or any equivalent data structure in any other language, it's like that. It's a collection of keys and values -- where keys are strings and values are anything (not accurate to say they are 'Objects' since not everything in JavaScript is an Object, like strings). var obj = new Object(); obj.foo = "string"; obj.bar = new Date(); obj["abc def"] = [1, 2, 3]; Which of course can be expressed a little more compactly: var obj = { foo: "string", bar: new Date(), "abc def": [1, 2, 3] }; JSON Web Services When you call a service that returns JSON, you end up with one of these "plain 'ole JavaScript objects". Plain, because there's nothing 'activate' about it. There's no code that runs within the object when you get and set values, etc. In this world of AJAX frameworks and features, it is just a plain object. But what if you are plugging that data into a component, which then consumes those values. If you ever change any of the values, the component won't know about it unless you tell it. Wouldn't it be nice if the component could be notified of any changes to the object automatically? That way, for example, if you were rendering out a list of Widgets based on an array of POJO objects containing widget data, you could, say, edit the name of one of the widgets and the rendered list could automatically reflect the updated value, without you having to force it or re-render everything. Doing it manually you may think wouldn't be so bad -- but it means coupling logic with UI, or at least deriving some custom mechanism for decoupling them. But there is no mechanism by which you can be notified of changes to a POJO. var widget = { name: "widget1" }; renderWidget(widget); widget.name = "newname"; renderWidget(widget); A possible solution would be to create an actual class to represent a Widget, and bind to that instead. Then instead of saying widget.name =" newname", you can call a method, like widget.set_name("newname"). The code could then execute a callback to notify anyone interested that there is a new name. The problem here is that the web service you called just returned a POJO. So now you have to convert the POJO to this business class object, or wrap it in one. If there is a lot of data, that could be quite expensive and slow, and it just complicates things. var widget = { name: "widget1" }; // came from a JSON service var businessObject = new Widget(widget); // warps or converts it renderWidget(businessObject); businessObject.set_name("newname"); Introducing Sys.Observer The Microsoft AJAX 4.0 preview includes this Sys.Observer class that allows you to be notified of updates to POJO's, provided you use its APIs to modify them. It is named observer of course after the Observer design pattern. Used internally[...]



Google Chrome - Aw, Snap!

Thu, 04 Sep 2008 05:32:00 GMT

Plenty of people have already seen this... interesting... error screen in chrome.

(image)

I wanted to see what a run-away script would do to Chrome. There's always a warning that the script is taking too long, giving you a chance to shut it down. Here's what Chrome does...

(image)

What is that icon? Is the page COLD or something? It's snowing! It's wearing a scarf! I guess the connection is that the page is "frozen."

EDIT: If I decide to "kill" it, they should have an animation of a spear going through it or something. Is it even acceptable to say you can "kill" the page? :)

Aw, snap!




Visual Studio Tip: Disable F1!

Fri, 18 Jul 2008 20:35:00 GMT

Lately I've been using a laptop more often than a desktop to work in Visual Studio, and of course like most laptop keyboards, the keys are jammed close together. The most annoying thing I keep doing by mistake is hitting F1 instead of Escape, which of course begins the not-so-quick process of bringing up help. Every time I do it, I sigh, wait, and close it.

No longer... realizing I can just disable F1 has made my day.

(image)

(image)

Now I can hit escape without fear.




My Latest Work

Wed, 18 Jun 2008 04:18:00 GMT

Sky Lynn Reed's 0th birthday is 5/15/2008 at 5:44pm, when she weighed 8 pounds, 0 ounces. A nice round binary number. Our first born! There are simply no words to describe the experience. We debated on whether we should have kids for a long time. After being married almost 5 years now, we finally decided to take the plunge. Now that she's here, and about 5 weeks old, we couldn't imagine our lives without her.

(image)

Of course, being a programmer daddy should be fun. We'll just see how early it is possible to learn to type. Imagine having an email address that you've had since before you were born? Oh, and a birthday cake with a number 0 candle on it makes sense to me. You're how old? Yeah, but you've only had that many traditional "birthdays". You've been robbed of the most important birthday there is -- your BIRTH day. As soon as she fits into it, I'll post a picture of her wearing her "Microsoft Future Developer" outfit. Dad is a geek, Sky, you better get used to it.

I look forward to just about everything that is to come. Of course it will be difficult. There will be major hurdles to jump over. Good times and bad times. But what a gift -- I get to live life all over again. I get to experience childhood as an adult.




Flickering UI From the ASP.NET AJAX Toolkit TabContainer while in an UpdatePanel?

Wed, 30 Apr 2008 18:57:00 GMT

UPDATE  This issue is officially fixed in .NET 3.5 SP1. If you have applied the workaround in this blog post, you no longer need it. But the story of this bug is still rather interesting.  UPDATE If you upgrade from Microsoft ASP.NET AJAX Extensions 1.0 to 3.5, and you have a TabContainer inside an update panel, like this:                                                              Tab                                                    Tab Content                                             ...you may notice a change in behavior. When the update panel updates, the UI seems to flicker off and on again, whereas with AJAX 1.0 it seemed to update instantly without any flicker. You may have noticed a similar problem with other components as well, depending on what they are doing. This is one of those interesting bugs with a convoluted history, driven by browser quirky behavior. The Tale -- First, some background Update panels basically work by replacing their content with the new content from the server by setting its innerHTML. However, there may also be components defined within that HTML that register script. Those components need to be recreated now that there is a new set of HTML. UpdatePanel runs those scripts by dynamically creating a new