asp.net声明:Inherits
Inherits: 定义供页继承的代码隐藏类。可以是从 Page 类派生的任何类。
在用户控件的声明里也有这个属性
ASP.NET中IsPostBack属性
通过页面的IsPostback属性,可以检查 .aspx 页是否为传递回服务器的页面;当加载页面并对控件的更改属性处理之前,用户可以在page_Load事件中检查该页面是否被传递回的页面。
一般是在page_load中检查是否是第一次加载本页或是判断是否是用户提交(PostBack)
if (!IsPostBack) {
//do something
}
asp.net IsPostBack深入解析
1 IsPostBack介绍
IsPostBack是Page类有一个bool类型的属性,用来判断针对当前Form的请求是第一次还是非第一次请求。当IsPostBack=true时表示非第一次请求,我们称为PostBack,当IsPostBack=false时表示第一次请求。在asp.net框架内部有很多的场景需要判断IsPostBack,比如LoadAllState等操作就需要在PostBack的时候进行。对于我们自己使用WebForm进行开发时,经常会在Page_Load中对IsPostBack进行判断,因为第一次请求的时候会执行Page_Load,在非第一次请求的时候也会执行Page_Load。为什么对同一个Form有多次请求呢?asp.net中引入了服务器端事件,支持服务器端事件的控件,会发出对当前Form的请求,这样在很多情形下我们就需要区别是否是对这个Form的第一次请求。
2 IsPostBack结论
本人对.Net的源代码中相关的处理进行的分析得到如下的结论:
结论① 对于使用Server.Transfer进行迁移时迁移到的页面其IsPostBack=false。
结论② Post方式如果Request中没有请求值,即Request.Form =null则IsPostBack=false;Get方式如果Request中没有请求值,即Request.QueryString =null则IsPostBack=false。
结论③ 如果QueryString或Form虽然有请求值,但是QueryString或Form中的Key没有“__VIEWSTATE”和“__EVENTTARGET”和“__VIEWSTATEFIELDCOUNT”,并且没有键为“null”,值以“__VIEWSTATE”开头并且也没有值为“__EVENTTARGET”的键值对,则IsPostBack=false。
结论④ 使用Response.Redirect方式向自画面迁移时,此时IsPostBack=false。
结论⑤ 发生跨页提交(CrossPagePostBack),当访问PreviousPage属性的时候,对于源Page,IsPostBack=true。
结论⑥ 发生跨页提交(CrossPagePostBack)时目标页面是IsPostBack=false
结论⑦ 使用Server.Execute迁移到的页面其IsPostBack=false。
结论⑧ 在Page运行期间其对应的DLL被更新了并且Page的树结构发生过变化,这种情况下请求时IsPostBack=false。
可以这样来理解这些结论:一般情况判断Request中如果没有请求值则IsPostBack=false。如果有请求值但是不包括“__VIEWSTATE”等一些特殊的键或值,则IsPostBack=false(每次请求后.Net框架会将一些特殊的隐藏域“__VIEWSTATE”等返回给客户端)。还有一些特殊的情形是上面的规则不能正确判断的需要特殊处理的,这些情形包括Server.Transfer,Response.Redirect,CrossPagePostBack,Server.Execute,发生了页面元素变化及重新编译。
一般来说记住上面的结论就可以,如果您有兴趣,或者怀疑请继续看下面的IsPostBack推论过程。
3 IsPostBack推论过程
下面是根据.Net框架中的源代码,来分析IsPostBack是如何判断出来的。对于这些结论的推断本人做了相关的试验来证明推论的正确性,由于篇幅的原因没有将这些试验代码体现出来。另外不可能将全部的.Net框架的代码都体现出来,只是将相关的代码片段列出,说明推断的依据。另外由于本人水平有限对.Net框架的代码理解还存在的不足的地方,请发现后进行指正,谢谢。
publicbool IsPostBack
{
get
{
if (this._requestValueCollection == null)
{
return false;
}
if (this._isCrossPagePostBack)
{
return true;
}
if (this_pageFlags[8])
{
return false;
}
return (
(
(this.Context.ServerExecuteDepth <= 0) ||
((this.Context.Handler != null) &&
(base.GetType() == this.Context.Handler.GetType()))
) && !this._fPageLayoutChanged
);
}
}
我们将每一个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。
评论