I'm writing a custom control in ASP.Net 3.5 WebForms, using ASP.Net AJAX. I'm building a state toggle into the control (expanded and collapsed modes). I've been trying to keep the current state of the control in ViewState, changing it in a server-side event handler manually:
object oExpanded = ViewState[ "Expandedness" ];
if(oExpanded == null)
{
oExpanded = ListState.Collapsed;
}
ListState lsCurrentState = (ListState) oExpanded;
if(lsCurrentState == ListState.Collapsed)
{
//code snipped - move to expanded mode here
ViewState[ "Expandedness" ] = ListState.Expanded;
}
else
{
//code snipped - move to collapsed mode here
ViewState[ "Expandedness" ] = ListState.Collapsed;
}
I don't think it should matter, but the actual rendering happens in an updatepanel and the code above is in an event handler that's triggered asynchronously. The problem I have is that on the next postback, ViewState["Expandedness"]
returns null. I've tried to use Fritz Onion's Viewstate Decoder tool to look at the actual contents of viewstate between postbacks, but it tells me the serialized data is invalid.
Have I taken a wrong approach here? What's the right way to tackle this problem?