Custom PageStatePersister and AJAX

There’s several useful custom page state persisters out there, which can help to get around the problem of huge viewstates.  These can do useful things like compress your viewstate or save it to disk and use a pointer.

One thing which is common in these implementations, is that they register a hidden field on the page to store the custom viewstate.  This is done using:

Page.ClientScript.RegisterHiddenField("__CUSTOMVIEWSTATEFIELD", viewState);

However, if you’re using this method, you will get some very unpredictable behaviour ajax behaviour with the standard asp.net ajax controls, like the UpdatePanel (the Telerik Ajax components all work perfectly).

The reason for this, is that the ViewState doesn’t get updated for ajax postbacks, as it’s now stored in a hidden field which isn’t updated when an ajax postback happens.

Suppose we have a linkbutton which toggles a panel’s visibility (on a page using a custom PageStatePersister with the above method of registering the hidden field).

protected void Toggle(object sender, EventArgs e)
{
 Panel1.Visible = !Panel1.Visible;
 Panel2.Visible = !Panel1.Visible;
}

 
 Panel1
 Panel2

When the page loads, Panel1 will be visible and Panel2 will be hidden. Clicking the ‘Toggle’ button will trigger an ajax postback toggling the two panels, making Panel1 hidden and Panel2 visible. However, clicking the button again will trigger an ajax postback but will not cause the panels visibility to toggle again.

The reason for this, is that the viewstate has become out of sync with the actual page, and therefore still thinks that Panel1 is visible and Panel2 is hidden.

Fortunately, the solution is simple. In your custom page state persister class, simply change the line above to:

ScriptManager.RegisterHiddenField(Page, "__CUSTOMVIEWSTATEFIELD", viewState);

This registers the new hidden field for storing viewstate with the ScriptManager, which then causes it to get updated with any ajax postbacks.

Remove annoying banner advert from Hotmail in IE7

I’ve posted before about removing the annoying banner advert from Hotmail/Windows Live Mail with Firefox and AdBlock+, but now you can do this with IE7 as well.

First download IE7Pro. This is an excellent IE7 add-on which adds numerous useful features, including an adblocker.  When installing, ensure that the ‘Enable user scripts’ option is checked.

Once installed, go to http://www.iescripts.org/view-styles-81p1.htm, and click on ‘install script’.  This will install an IE7Pro user script to remove the banner advert from the top of the screen and collapse the surrounding div.

This significantly improves the user experience of using Hotmail in IE7.  That big banner at the top really annoys me. Good riddance!

Hotmail, Firefox and Adblock+

For some strange reason, the Windows Live Mail Desktop Beta has stopped working with Hotmail on my machine, so I’ve had to revert to checking my account through a browser.

If you’re using Firefox and AdBlock+, and want to get from this:

Hotmail with banner

… to this …

Hotmail without advert banner

Simply add the following rule to AdBlock+

live.com#div(id*=RadAd)

Clicking on refresh (in the browser), or on the Hotmail button in the toolbar sometimes causes Firefox to crash, but the experience without the big annoying animated banner at the top makes the random crash worth it.

[Found this in the AdBlock+ forums]

ViewState problems and possible workarounds?

I’ve been working on a project recently, where one of the pages has a problem with the viewstate getting very big.  The page in question is a search results page, with lots of custom controls – user controls, composite controls and subclassed asp.net controls, all of which need to retain their state for some reason or another.

In about 0.8 – 1% of searches, the page throws the error:

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.

I’ve tried various optimisations, disabling viewstate on some controls and moving things around hasn’t made much of a difference.

Found a useful article about ViewState compression on CodeProject. Unfortunately, it hasn’t solved the problem, but it has cut the viewstate size significantly (~50%).

There’s also a viewstate chunking mechanism built into ASP.NET 2.0, allowing a web.config setting to be specified, which breaks the single hidden viewstate field into multiple hidden fields.  More details on Lauren Duveau’s blog.

It’s unlikely that this plays nicely with a custom page state persister (like the one used to compress the page viewstate), and although there are possible solutions, they don’t seem particularly elegant.

Dave Reed has a good article about truly understanding viewstate.

I’ve only got a couple of custom composite controls using dynamic controls, and although these are added in the OnInit handler (as recommended), I suspect the ’failed to load viewstate’ error is linked to these. Perhaps changing these to user controls where the dynamic controls are added declaratively instead of programatically will yield better results…

Update: subclassing my control to inherit from Image seems to have solved the problem. Previously, it was a composite control adding Image and Hyperlink controls in the OnInit event.  Instead, it inherits from Image now, and the Render event is overriden to render an A tag around the image with the hyperlink.

Channel9 gets silverlight

It’s been a while coming, but I’m glad to see that Channel9 got Silverlight and finally allows videos to be watched in the browser, rather than having to download large video files, which is a much better user experience.

Would’ve been good if they implemented this earlier… the technology has been available in Flash for ages, but better late than never.  I’ll definitely be watching a lot more videos on Channel9!

Now if only BBC would sort their act out and start offering streaming video. Their existing RealPlayer and Window Media offerings completely suck.  I’ve got issues with installing trash like RealPlayer (and Quicktime) on my machine, and the Windows Media Player content doesn’t seem to work in Firefox. At least they’ve mad a start, and are doing it for some content.

What if Google controlled your life?

Not the first and probably not the last story on how things could be if Google were evil, but good (scary!) reading anyway.

Radar Fiction – Scroogled. Google controls your email, your videos, your calendar, your searches… What if it controlled your life?

[Via Bill's House O Insomnia]