ASP.NET, XML, SQL and Javascript tips and tricks

One of our projects consists of single parent page and “widgets” that display secondary (classic ASPX webform) pages. A recent feature request was to auto-refresh widget information at given intervals. On the surface it was pretty straghtforward:

The problem with this approach – there’s an ugly flicker of white between page refresh and the goal was to keep displaying current IFRAME content up until refreshed content is ready. Yes, there’re various AJAX-ified methods (including ASP.NET UpdatePanel) – but they add unnecessary overhead and may cause other issues.

The solution was suggested by this Stack Overflow post. The idea is to have secondary, hidden IFRAME into which perform actual load. Once load complete – switch IFRAMES – currently visible with old content becomes hidden, and hidden one with new content becomes visible. It goes something like this:

This is sufficient for most of the cases but if your page continues to build in client-side JavaScript after load is completed – this may not be enough. You may need to hook into that JavaScript to determine when it finished to do visible/hidden IFRAME swap.

Case in point: Fusion Charts. It’s a super-cool HTML5/JS charting library, but, depending on complexity of your chart it may take noticeable time to render the chart. Fortunately it provides “rendered” event so you know when chart is ready. With this in mind here is augmented solution. Note also that instead of "display:none" to hide the iframe I use "position: absolute;top: -1000px" to move it outside of viewport. This is due to Fusion Charts don’t render correctly if their container is hidden. But the effect is the same – iframe is invisible until it’s needed.

This code is similar to previous one, the difference is (besides using absolute positioning to hide iframe) is we just assigning iframe source, we don’t use “onload” event to swap visibility of iframes. This is the job of JavaScript of “widget.aspx”.

Fusion Chart constructor function accepts an object specifying chart properties, data source and even actual data. One of the object properties is “events”. It specifies various events from chart life-cycle. We’re interested in “rendered” event, which signals chart is done:

Here widget code does what we did previously in “onload” event in the parent. When chart finishes rendering it hides first iframe (via absolute positioning) and makes second iframe visible. And on the next call roles are reversed. And the cycle continues.