我正在使用 ASP.Net AJAX 在 ASP.Net 3.5 WebForms 中编写自定义控件。我正在为控件构建一个状态切换(展开和折叠模式)。我一直在尝试将控件的当前状态保留在 ViewState 中,并在服务器端事件处理程序中手动更改它:
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;
}
我认为这无关紧要,但实际渲染发生在更新面板中,并且上面的代码位于异步触发的事件处理程序中。我遇到的问题是,在下一次回发时,ViewState["Expandedness"]
返回 null。我尝试使用 Fritz Onion 的 Viewstate Decoder 工具来查看回发之间 viewstate 的实际内容,但它告诉我序列化数据无效。
我在这里采取了错误的方法吗?解决这个问题的正确方法是什么?