Archive for October, 2008

TabContainer LoadClientState Failed

October 24, 2008

We have a set of small Javascript functions that retain the ActiveIndex of an ASP Ajax Toolkit TabContainer control and restores it on postback. Sometimes, perhaps due to increased sunspot activity, our TabContainer ClientState disappears. Resulting in the intruiging and quite humorous error “When casting from a number, the value must be a number less than infinity.”

To this day I have no idea what causes TabContainer to lose its state, but courtesy of a fellow traveller in pain, I can present a workaround.

Meet The Javascripts

Here’s our happy little Javascript family from the picturesque village of MarkUp

In Head


function saveCurrentTabIndex()
{
var tabcontainer = $get('').control;
var tabIndex = $get('');
if (tabcontainer != null)
{
tabIndex.value = tabcontainer.get_activeTabIndex();
}
}

function restoreCurrentTabIndex()
{
var tabIndex = $get('tabIndexHidden');
if (tabIndex.value > -1)
{
var tabcontainer = $find( '');
var index = tabcontainer.get_activeTabIndex();
if (index != tabIndex.value)
{
tabcontainer.set_activeTabIndex(tabIndex.value);
}
}
}

In Body
input type=”hidden” id=”tabIndexHidden” runat=”server”

Last thing in markup:


function pageLoad(sender,e)
{
restoreCurrentTabIndex();
}

Family Dysfunction

So, like I said, sometimes TabContainer ClientState is null causing the bizarre ‘number must be less than infinity’ error message, I assume because null is not less than infinity. To work around it use the Javascript parseInt function to make sure TabContainer has an ActiveTabIndex it can grok.

Resolving Our Little Differences

In saveCurrentTabIndex:
tabcontainer.set_activeTabIndex(parseInt(tabIndex.value));

Now the ActiveTabIndex will be less than Infinity to the relief of all concerned with the possible exception of Buzz Lightyear.

Our Family Counsellor can be located here.. Thanks djs25uk 🙂