在Page.ProcessRequestMain(bool, bool)中有如下的代码:
if (this.PageAdapter != null)
{
this._requestValueCollection = this.PageAdapter.DeterminePostBackMode();
}
else
{
this._requestValueCollection = this.DeterminePostBackMode();
}
PageAdapter.DeterminePostBackMode最终还是调用了Page.DeterminePostBackMode,下面我们看Page.DeterminePostBackMode如何实现。
protected internal virtual NameValueCollection DeterminePostBackMode()
{
if (this.Context.Request == null)
{
return null;
}
if (this.Context.PreventPostback)
{
return null;
}
NameValueCollection collectionBasedOnMethod = this.GetCollectionBasedOnMethod(false);
if (collectionBasedOnMethod == null)
{
return null;
}
bool flag = false;
string values = collectionBasedOnMethod.GetValues((string) null);
if (values != null)
{
int length = values.Length;
for (int i = 0; i < length; i++)
{
if (values[i].StartsWith("__VIEWSTATE", StringComparison.Ordinal) ||
(values[i] == "__EVENTTARGET"))
{
flag = true;
break;
}
}
}
if (((collectionBasedOnMethod["__VIEWSTATE"] == null) && (collectionBasedOnMethod["__VIEWSTATEFIELDCOUNT"] == null)) &&
((collectionBasedOnMethod["__EVENTTARGET"] == null) && !flag))
{
return null;
}
if (this.Request.QueryStringText.IndexOf(
HttpResponse.RedirectQueryStringAssignment, StringComparison.Ordinal) != -1)
{
collectionBasedOnMethod = null;
}
return collectionBasedOnMethod;
}
这个函数中返回null就意味者IsPostBack=false,将上面函数中每个返回为null的地方作如下的分析。
3.1.1 this.Context.Request == null
if (this.Context.Request == null) { return null; }this.Context.Request == null应该只有在异常的情况下会发生,
正常情况下会在HttpRuntime.ProcessRequestInternal中创建HttpContext及HttpRequest对象。
3.1.2 this.Context.PreventPostback
if (this.Context.PreventPostback) { return null; }在HttpServerUtility.Transfer中会使用PreventPostback,其代
码如下:
public void Transfer(string path)
{
bool preventPostback = this._context.PreventPostback;
this._context.PreventPostback = true;
this.Transfer(path, true);
this._context.PreventPostback = preventPostback;
}
在调用Server.Transfer进行画面迁移时设置Context.PreventPostback=ture。此处得出结论①:对于使用Server.Transfer进行迁移时迁移到
的页面其IsPostBack=false。
3.1.3 collectionBasedOnMethod == null
NameValueCollection collectionBasedOnMethod = this.GetCollectionBasedOnMethod(false);
if (collectionBasedOnMethod == null)
{
return null;
}
调用了Page.GetCollectionBasedOnMethod后其返回值进行判断。如果其返回值为null则IsPostBack为false。
Page.GetCollectionBasedOnMethod的定义如下:
internal NameValueCollection GetCollectionBasedOnMethod(bool dontReturnNull)
{
if (this._request.HttpVerb == HttpVerb.POST)
{
if (!dontReturnNull && !this._request.HasForm)
{
return null;
}
return this._request.Form;
}
if (!dontReturnNull && !this._request.HasQueryString)
{
return null;
}
return this._request.QueryString;
}
从上面的代码可以看出返回值为null的情形是_request.HasForm=null或_request.HasQueryString=null。此处得出结论②:Post方式