【问题标题】:How does IsPostback technically work?IsPostback 在技术上是如何工作的?
【发布时间】:2011-08-04 18:26:06
【问题描述】:

我目前遇到一个奇怪的问题,即当我单击一个简单地回发到同一页面的 asp.net 按钮时,除 Google Chrome 之外的所有浏览器都在 Page_Load 事件中注册对 IsPostback 的调用为真。

这使我尝试发现 ASP .Net 页面中的 IsPostback 属性在技术上是如何实现的,而我正在努力寻找。

到目前为止,我的想法是它可能与以下内容有关;

  • 请求 VERB 类型是 POST 而不是 GET。
  • 包含 Viewstate 信息的隐藏输入不存在任何信息,因此之前提交的控制信息不可用。
  • 请求标头中的 http referer 与当前 URL 相同。

谁能提供用于确定 IsPostback 布尔属性的条件的实际细分?

注意:我正在寻找实际实施而不是看法/理论,因为我希望使用它来积极解决问题。我还搜索了 MSDN,但迄今为止找不到任何准确涵盖该机制的技术文章。

提前致谢, 布赖恩。

【问题讨论】:

    标签: asp.net http-headers httpverbs ispostback


    【解决方案1】:

    页面查找是否存在__PREVIOUSPAGE 表单值。

    来自反射器:

    public bool IsPostBack
    {
        get
        {   //_requestValueCollection = Form or Querystring name/value pairs
            if (this._requestValueCollection == null)
            {
                return false;
            }
    
            //_isCrossPagePostBack = _requestValueCollection["__PREVIOUSPAGE"] != null
            if (this._isCrossPagePostBack)
            {
                return true;
            }
    
            //_pageFlags[8] = this._requestValueCollection["__PREVIOUSPAGE"] == null
            if (this._pageFlags[8])
            {
                return false;
            }
    
            return (   ((this.Context.ServerExecuteDepth <= 0) 
                    || (   (this.Context.Handler != null) 
                        && !(base.GetType() != this.Context.Handler.GetType())))
                    && !this._fPageLayoutChanged);
        }
    }
    

    【讨论】:

    • 感谢反映的输出,赞成。我想我仍在寻找更明确但更高级别的描述。这向我展示了实际的属性实现,但没有提供 ASP .Net 如何与浏览器请求交互以确定 IsPostback 的知识。以“this._pageFlags[8]”为例,这在整体机制方面有何特别意义?
    • 太棒了,谢谢。感谢您和 Prescott 所花费的时间。
    【解决方案2】:

    回发实际上很简单,只需将表单提交给它自己(大部分情况下)。 javascript代码实际上是放在你的页面上的:

    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
    <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
    
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
    

    标记答案向您显示正在运行的服务器端代码。

    【讨论】:

    • 谢谢,这对我的理解更有帮助。
    【解决方案3】:

    Is Postback 是这样实现的(使用 Reflector):

    public bool get_IsPostBack()
    {
        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);
    }
    

    因此,除非您考虑到所有这些参数,否则无法对其进行跟踪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 2016-07-12
      • 2011-04-05
      • 2015-03-09
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多