龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

.NET中IsPostBack介绍:详细分析Page_load使用IsPostBack的原因(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
我们将每一个if判断作为一个小节,作如下的分析。 3.1 this._requestValueCollection == null if (this._requestValueCollection == null) { return false; } 可以看出_requestValueColle

我们将每一个if判断作为一个小节,作如下的分析。

3.1 this._requestValueCollection == null

if (this._requestValueCollection == null)

{

return false;

}

可以看出_requestValueCollection等于null时IsPostBack就等于false。

在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;

}


精彩图集

赞助商链接