當你的程序出問題時, 不僅僅要檢查自己的代碼和邏輯, 還要檢查微軟的代碼, 累嗎? 原文在: http://forums.ASP.Net/t/1192141.ASPx , 轉給這裡.
if you cache two usercontrols in a page, and both of which contains either a menu or treevIEw(or any control that injects embeded CSS style into parent page), then the first time you get the response right, but on second request or any later request where the cached content is used, the outout will not be correct. inspecting the Html source, I found some inline style definition is missing for the later response. It seems this is a bug in ASP.Net: BasePartialCachingControl.PreRenderRecursiveInternal(), where only the last cached style output is used, and any previous style string set by other cached usercontrols positioned earlIEr in the page is overwritten.
Besides the senerio of two side by side usercontrols being cached, there is another senerio, suppose one usercontrol uses embedded style(both menu and treevIEw use these, and many other AJax controls also use this technique to inject style definition into the Html head element), and it contains a child usercontrol who is also set to cached, and also uses embedded style, the two controls expiration event will ussually not coincident, and now suppose that the parent one is being regenerated while the child one has still a valid cached version, in this case, since the child one''s css settings will go through the path of page.header.StyleSheet.CSSStyleString (in BasePartialCachingControl.PreRenderRecursiveInternal), so it will not be merged with the parent control''s css setting, which results an incorrect CSS setting string being stored into cache, so any subsequence output of the parent usercontrol from cache will be incorrect.
I once wondered if this had been fixed in latest service pack, so downloaded .Net framework 2.0 service pack 1, unfortunately, it was not.
hope someone can verify the above, and if verifIEd a bug, hope Microsoft can fix this bug.
a suggested fix for it:
in basepartial, add a childrenCSSRawstring property
in basepartial.GetCssStyleRenderString(), append childrenCSSRawstring to the final output
change RegisterCSSStyleString to:
internal void StyleSheetInternal.RegisterCSSStyleString(string outputString)
{
((StyleSheetInternal) this.StyleSheet).CSSStyleString +="\r\n"+ outputString;
Page page = this._owner.Page;
if (page.PartialCachingControlStack != null)
{
foreach (BasePartialCachingControl control in page.PartialCachingControlStack)
{
control.appendCSSRawString(outputString);
}
}
}