我想知道如何在服务器端检测到回发?
通常,页面上的控件状态存储在Viewstate中,并且每次回发到服务器时都会来回遍历。
由于http是无状态的,服务器如何区分postback
ed页面和初始页面加载。
我们可以使用Page.IsPostback属性,它自动设置为true或false,分别用于回发和初始加载。但我的问题是什么控制了Page.Ispostback属性的true和false赋值以及服务器如何找出表单回发? 服务器是否使用任何隐藏字段来检测页面的回发?
答案 0 :(得分:0)
答案 1 :(得分:0)
有点复杂。 System.Web.UI.Page
的权重大约为6500行,具有众多公共/内部依赖关系。
http://referencesource.microsoft.com/#System.Web/UI/Page.cs
来自您之前的评论:
就像我们预期的那样,客户端向服务器发送一些隐藏字段 检测回发并基于此将IsPostback设置为true。
您使用隐藏字段是正确的。 IrishChieftain在指出HTTP动词用于判断时也是正确的。
整个过程是无国籍的。
Page
通过一系列步骤来做出决定。
Starting with ProcessRequestMain():
PageAdapter
。PageAdapter's
DeterminePostBackMode()
方法,或在页面上调用DeterminePostBackMode()
。GetCollectionBasedOnMethod()
Request.Form
集合。GetCollectionBasedOnMethod()
获得了一个收藏品?如果是这样检查某些隐藏的字段。_pageFlags[isCrossPagePostRequest]
。这些似乎是调用IsPostBack
所必需的先决条件。
public bool IsPostBack {
get {
if (_requestValueCollection == null)
return false;
// Treat it as postback if the page is created thru cross page postback.
if (_isCrossPagePostBack)
return true;
// Don't treat it as a postback if the page is posted from cross page
if (_pageFlags[isCrossPagePostRequest])
return false;
// Don't treat it as a postback if a view state MAC check failed and we
// simply ate the exception.
if (ViewStateMacValidationErrorWasSuppressed)
return false;
// If we're in a Transfer/Execute, never treat as postback (ASURT 121000)
// Unless we are being transfered back to the original page, in which case
// it is ok to treat it as a postback (VSWhidbey 117747)
// Note that Context.Handler could be null (VSWhidbey 159775)
if (Context.ServerExecuteDepth > 0 &&
(Context.Handler == null || GetType() != Context.Handler.GetType())) {
return false;
}
// If the page control layout has changed, pretend that we are in
// a non-postback situation.
return !_fPageLayoutChanged;
}
}
答案 2 :(得分:0)
System.Web.UI.Page类使用各种方法检查当前请求是否为回发(设置IsPostBack属性)。其中一些(不是全部)是:
可以从source code for System.Web.UI.Page.IsPostBack属性
获取更多信息此源代码的Visual Studio解决方案也适用于download