希望写下这样的logic对大家做troubleshooting有所帮助。我们可以这样分析这个问题:
第一步,简化一下page。建一个新的asp.net web application,用下面的code:
private void page_load(object sender, system.eventargs e)
{
dropdownlist ddldynamic = new dropdownlist();
ddldynamic.id = "ddldynamic";
htmlform form1 = (htmlform)this.findcontrol("form1");
if (!ispostback)
{
ddldynamic.items.add("before");
}
form1.controls.add(ddldynamic);
if (!ispostback)
{
ddldynamic.items.add("after");
}
}
在page上扔个button,以便可以postback。运行后postback的结果,“before” item没被保留,“after”被保留了。问题被isolate:问题不在于dropdownlist或者listcollection对于view state的处理出问题,而是特定一个listitem view state的处理有异。
现在有目标了,接下来看listitem source code:
internal object saveviewstate()
{
if (this.misc.get(2) && this.misc.get(3))
{
return new pair(this.text, this.value);
}
if (this.misc.get(2))
{
return this.text;
}
if (this.misc.get(3))
{
return new pair(null, this.value);
}
return null;
}
可以看到只有misc.get(2)或misc.get(3)符合一定条件才存view state,鉴于misc是private member,继续在listitem的code里找什么会影响misc.get(2) or misc.get(3)的值,结果如下:
internal bool dirty
{
set
{
this.misc.set(2, value);
this.misc.set(3, value);
}
}
找到了唯一的可能,在reflector里看set方法的callee graph,找到system.web.ui.webcontrols.listitemcollection.add(listitem):void方法。接续看source code:
public void add(listitem item)
{
this.listitems.add(item);
if (this.marked)
{
item.dirty = true;
}
}
这里有一个private bool marked flag。继续在listitemcollection里找:
internal void trackviewstate()
{
this.marked = true;
for (int num1 = 0; num1 < this.count; num1++)
{
this[num1].trackviewstate();
}
}
void istatemanager.trackviewstate()
{
this.trackviewstate();
}
好了,看来这个方法就是关键了……由于是interface的方法,我们可以尝试一下在page2里调用它:
((istatemanager)(ddldynamic.items)).trackviewstate();
if (!ispostback)
{
for (int i=1; i <=3; i++)
ddldynamic.items.add(new listitem(i.tostring(), i.tostring()));
}
form1.controls.add(ddldynamic);
确实是起作用了……鉴于如此,我们可以猜测controlcollection.add一定调用了system.web.ui.webcontrols.listitemcollection.trackviewstate()这个方法。要证明这点容易多了……
用windbg,在system.web.ui.webcontrols.listitemcollection.trackviewstate()方法上设个断点。call stack如下:
019cf8b0 06538fd0 [default] [hasthis] void system.web.ui.webcontrols.listitemcollection.trackviewstate()
019cf8b4 06538fbe [default] [hasthis] void system.web.ui.webcontrols.listcontrol.trackviewstate()
019cf8bc 06538e53 [default] [hasthis] void system.web.ui.control.initrecursive(class system.web.ui.control)
019cf8d8 0653758a [default] [hasthis] void system.web.ui.control.addedcontrol(class system.web.ui.control,i4)
019cf8f4 06537462 [default] [hasthis] void system.web.ui.controlcollection.add(class system.web.ui.control)
019cf904 063c06fc [default] [hasthis] void webapplication37.webform3.page_load(object,class system.eventargs)
at [+0x13c] [+0x8c] c:\inetpub\wwwroot\webapplication37\webform3.aspx.cs:36
019cf944 065391a4 [default] [hasthis] void system.web.ui.control.onload(class system.eventargs)
…
好了……一切都明了……
