文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页技术开发.Net 专栏 → IsPostBack深入分析

IsPostBack深入分析

时间:2010/2/3 11:54:00来源:本站整理作者:我要评论(0)

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框架的代码理解还存在的不足的地方,请发现后进行指正,谢谢。 

public bool 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。 

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方式如果 Request中没有请求值,即 Request.Form =null则 IsPostBack= false; Get方式如果 Request中没有请求值,即 Request.QueryString =null则 IsPostBack= false。 

3.1.4   ((collectionBasedOnMethod["__VIEWSTATE"] == null) && (collectionBasedOnMethod["__VIEWSTATEFIELDCOUNT"] == null)) && ((collectionBasedOnMethod["__EVENTTARGET"] == null) && !flag) 

    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 ; 

            } 

        } 

    } 

上面这段代码的意思是判断请求的键值对中是否存在没有键,其值以“__VIEWSTATE”开头或者其值为“__EVENTTARGET”。例如如下的Get请求方式会使得flag=true。 

…/defalt.aspx?__VIEWSTATE 

…/defalt.aspx?__EVENTTARGET 

对于Get方式“?__VIEWSTATE=”会将__VIEWSTATE作为请求的键,其值为“”,但是“?__VIEWSTATE”会认为其键为“null”,其值为“__VIEWSTATE” 

if ( 

((collectionBasedOnMethod["__VIEWSTATE"] == null ) && (collectionBasedOnMethod["__VIEWSTATEFIELDCOUNT"] == null )) && ((collectionBasedOnMethod["__EVENTTARGET"] == null ) && !flag)) 



return null ; 



如上的条件意味着请求的键中同时没有“__VIEWSTATE”,“__EVENTTARGET”,“__VIEWSTATEFIELDCOUNT”,并且flag为 false则返回null。flag为false意味着没有键为“null”值以“__VIEWSTATE”开头并且也没有值为 “__EVENTTARGET”的键值对。 

此处得出结论③如果QueryString或Form虽然有请求值,但是QueryString或Form中的Key没有“__VIEWSTATE”和 “__EVENTTARGET”和“__VIEWSTATEFIELDCOUNT”,并且没有键为“null”值以“__VIEWSTATE”开头并且也没有值为“__EVENTTARGET”的键值对,则IsPostBack=false。 

3.1.5 this.Request.QueryStringText.IndexOf(HttpResponse.RedirectQueryStringAssignment, StringComparison.Ordinal) != -1 

if (this .Request.QueryStringText.IndexOf(HttpResponse .RedirectQueryStringAssignment, StringComparison .Ordinal) != -1) 



collectionBasedOnMethod = null ; 



HttpResponse.RedirectQueryStringAssignment 的值为“__redir=1”,上面的代码的意思是如果QueryStringText中包括包括“__redir=1”则返回null。在 HttpRequest.Redirect中会判断如果IsPostBack为true,并且URL中不包含有“__redir=1”时,会给URL中增加“__redir=1”。一般情况下我们使用request.Redirect迁移到的页面都应该是IsPostBack=false,有一种特殊的情形是使用request.Redirect迁移到当前页,此时IsPostBack为true。此种情况发生时在request.Redirect中给 URL中增加“__redir=1”。执行到page. ProcessRequestMain时会重新将IsPostBack判断为fales。 

此处得出结论④ 使用 Response.Redirect方式向自画面迁移时,此时 IsPostBack =false。 

此时大家可能会有疑问为什么 使用 Response.Redirect方式向自画面迁移时要特殊处理,使用 Response.Redirect向其他画面迁移为什么不要。使用 Response.Redirect 向其他画面迁移时Response.Form=null,Response.QueryString=null,所以可以判断是 IsPostBack=false。但是使用Response.Redirect方式向自画面迁移时 Response.QueryString<>null,所以要特殊判断。 

3.2 this._isCrossPagePostBack 

        if (this ._isCrossPagePostBack ) 

        {

            return true ; 

        }

在Page的PreviousPage属性中会对_isCrossPagePostBack进行设置,具体代码如下: 

public Page PreviousPage 



    get 

    { 

             … 

ITypedWebObjectFactory vPathBuildResult = (ITypedWebObjectFactory ) BuildManager .GetVPathBuildResult(this .Context, this ._previousPagePath); 

            if (typeof (Page ).IsAssignableFrom(vPathBuildResult.InstantiatedType)) 

            { 

                this ._previousPage = (Page ) vPathBuildResult.CreateInstance(); 

                this ._previousPage._isCrossPagePostBack = true ; 

                this .Server.Execute(this ._previousPage, TextWriter .Null, true , false ); 

            } 

        } 

        return this ._previousPage; 

    } 



在发生跨页面提交的时候,当访问PreviousPage属性的时候源Page的IsCrossPagePostBack会被设置true。此处得出结论⑤ 发生跨页提交( CrossPagePostBack),当访问 PreviousPage属性的时候,对于源 Page, IsPostBack=true。 

3.3 this._pageFlags[8] 

        if (this . _pageFlags [8]) 

        {

            return false ; 

        }

在Page. ProcessRequestMain中有如下的代码片断对_pageFlags[8]进行赋值。 

else if (!this .IsCrossPagePostBack) 



    VirtualPath path = null ; 

    if (this._requestValueCollection ["__PREVIOUSPAGE" ] != null ) 

    { 

        try 

        { 

            path = VirtualPath .CreateNonRelativeAllowNull( 

DecryptString(this._requestValueCollection ["__PREVIOUSPAGE" ])); 

        } 

        catch (CryptographicException ) 

        { 

            this ._pageFlags[8] = true ; 

        } 

        if ((path != null ) && (path != this .Request.CurrentExecutionFilePathObject)) 

        { 

            this ._pageFlags[8] = true ; 

            this ._previousPagePath = path; 

        } 

    } 



解密发生异常时_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 。

 

相关视频

    没有数据

相关阅读 利用Page.IsPostBack属性保持用户输入的Frameworklistary使用教程 listary的使用技巧NBA2K20配置要求高吗 NBA2K20配置要求推荐iPhone数据迁移怎么用 iOS 12.4数据迁移功能使用教程iOS 12.4更新了什么 iOS 12.4更新内容一览iOS 12.4下载 12.4固件下载地址Steam免费游戏怎么领 Steam免费游戏一键领取教程NBA2K20普通和豪华版区别 NBA2K20豪华版预购奖励介绍

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 什么是.NET中的TDD?ASP.NET AJAX入门简介 WebMatrix入门教程VC++2008中如何调用GetOpenFileName打开文件PlaySound函数在VC++6.0中如何播放音乐及声请问VC++回调函数怎么用

人气排行 嵌入式实时操作系统VxWorks入门教程ArrayList 与 string、string[] 的转换C#遍历整个文件夹及子目录的文件代码WebMatrix入门教程asp.net判断文件或文件夹是否存在c#判断数据NULL值的方法vc++6.0怎么写Windows简单窗口代码.net解决数据导出excel时的格式问题