.NET中IsPostBack介绍:详细分析Page_load使用IsPostBack的原因(4)
解密发生异常时_pageFlags[8]为true这种异常发生的可能性比较小我们忽略,重点看另外一种情形,将这种情形的所有条件结合起来就是
IsCrossPagePostBack=false && _requestValueCollection["__PREVIOUSPAGE"] != null && path != null && (path !=
this.Request.CurrentExecutionFilePathObject)。发生跨页提交时对于目标页面IsCrossPagePostBack=false,此时源页面
的"__PREVIOUSPAGE"等信息会提交给目标页面,所以_requestValueCollection["__PREVIOUSPAGE"] != null。此时当前请求的
CurrentExecutionFilePathObject是根据目标页的路径生成的,与使用_requestValueCollection["__PREVIOUSPAGE"]生成的path对象不同。
此处得出结论⑥发生跨页提交(CrossPagePostBack)时目标页面是IsPostBack=false。为什么需要对CrossPagePostBack的目标页面做这样的
处理呢?发生CrossPagePostBack时,会将源页面的信息提交给目标页面此时Request.Form!=null,而且包括__VIEWSTATE等键按照其他的规
则会判断为IsPostBack=true,所以需要对CrossPagePostBack的目标页面做特殊的判断。
3.4 (this.Context.ServerExecuteDepth <= 0) || ((this.Context.Handler != null) && (base.GetType() ==
this.Context.Handler.GetType()))
在HttpServerUtility中有如下的代码对Context. ServerExecuteDepth进行了操作。
public void Execute(string path, TextWriter writer, bool preserveForm)
{
…
try
{
this._context.ServerExecuteDepth++;
handler = this._context.ApplicationInstance.MapHttpHandler(this._context, request.RequestType, path3, filename,
useAppConfig);
}
finally
{
this._context.ServerExecuteDepth--;
}
…
}
在HttpServerUtility.ExecuteInternal中也有一处对Context.ServerExecuteDepth类似的操作。HttpServerUtility.Execute会调用
HttpServerUtility.ExecuteInternal。从此可以看出Context.ServerExecuteDepth是表示Server.Execute中的执行深度。在调用
Server.Execute时Context.ServerExecuteDepth>0。另外调用Server.Execute后Context.Handle中存储的还是原来的页对象,也就是说
base.GetType()!= this.Context.Handler.GetType()。这样对于Server.Execute来说this.Context.ServerExecuteDepth <= 0) ||
((this.Context.Handler != null)这个条件为false。此处得出结论⑦使用Server.Execute迁移到的页面其IsPostBack=false。此处我们会有
疑问,为什么需要对Server.Execute进行特殊的判断呢?理由是使用Server.Execute时会将源Page中的隐含域提交,此时Request.Form!=null
,而且包括__VIEWSTATE等键按照其他的规则会判断为IsPostBack=true。
3.5 this._fPageLayoutChanged
fPageLayoutChanged从这个变量的字面意思来看是Page的Layout发生了变化。
在Page.LaodAllState中代码片断如下:
private void LoadAllState()
{
…
string s = (string) second.First;
int num = int.Parse(s, NumberFormatInfo.InvariantInfo);
this._fPageLayoutChanged = num != this.GetTypeHashCode();
…
}
其意思是现在得到的HashCode和存储在ViewState中的HashCode不一致时fPageLayoutChanged=true。GetTypeHashCode()会返回一个HashCode
,而且这个方法是对aspx进行编译的时候产生的,只有在页面上的元素发生了变化的时候其返回的值会发生变化。此处得出结论⑧在Page运行
期间其对应的DLL被更新了并且Page的树结构发生过变化,这种情况下请求时IsPostBack=false。