How does IsPostback technically work?

浪子不回头ぞ 提交于 2019-11-26 17:44:22

问题


I'm currently having a strange issue whereby all browsers except from Google Chrome are registering a call to IsPostback within a Page_Load event as true when I click an asp.net button which simply posts back to the same page.

This has led me to try and discover how the IsPostback property within an ASP .Net page is technically implemented, something I'm struggling to find.

My thoughts to date are that it could be related to the following;

  • The request VERB type is POST rather than GET.
  • The hidden input containing the Viewstate information has no information present and therefore no previously submitted control information is available.
  • The http referer in the request headers is the same as the current URL.

Can anyone provide an actual breakdown of the conditions used to determine the IsPostback boolean property?

Note: I'm looking for the actual implementation rather than perceptions / theory as I'm hoping to use this to actively resolve an issue. I've also searched MSDN and to date cannot find any technical article accurately covering the mechanism.

Thanks in advance, Brian.


回答1:


The page looks for the existence of a __PREVIOUSPAGE form value.

From Reflector:

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);
    }
}



回答2:


Postback actually works fairly simply by submitting the form to itself (for the most part). The javascript code is actually put on your page:

<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();
    }
}

Marks answer shows you the server side code that is run.




回答3:


Is Postback is implemented as such (using 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);
}

So unless you take into account all these parameters, it will not be possible to trace it.



来源:https://stackoverflow.com/questions/5650580/how-does-ispostback-technically-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!