开发者

ASP.NET AJAX weirdness

开发者 https://www.devze.com 2023-01-01 18:58 出处:网络
Ok, I thought I understood these topics well, but I guess not, so hopefully someone here can clear this up.

Ok, I thought I understood these topics well, but I guess not, so hopefully someone here can clear this up.

Page.IsAsync seems to be broken. It always returns false.

But ScriptManager.IsInAsyncPostBack seems to work, sort of.

It returns true during the round trip for controls inside UpdatePanels. This is good; I can tell if it's a partial postback or a regular one.

ScriptManager.IsInAsyncPostBack ret开发者_如何学运维urns false however for async Page Methods. Why is this? It's not a regular postback, I'm just calling a public static method on the page.

It causes a problem because I also realized that if you have a control with AutoPostBack = false, it won't trigger a postback on it's own, but if it has an event handler on the page, that event handler code WILL run on the next postback, regardless of how the postback occurred, IF the value has changed. i.e. if I tweak a dropdown and then hit a button, that dropdown's handler code will fire. This is ok, except that it will also happen during Page Method calls, and I have no way to know the difference.

Any thoughts?


As Tjaart points out, Page.IsAsync has nothing to do with AJAX! See MSDN for a bit more info about IsAsync and see http://msdn.microsoft.com/en-us/magazine/cc163725.aspx for a fuller description of async pages].

Page methods are web services by a different name. The ScriptManager will emit the necessary JS boiler plate to make creating an XHR that invokes the web service very easy but that's all ScriptManager has to do with them really.

As the MSDN page states, ScriptManager.IsInAsyncPostBack will only be true if the request is in "partial rendering mode" so ScriptManager.IsInAsyncPostBack will be false when you are executing a page method because that request has not been spawned as a result of a partial postback (i.e. an UpdatePanel refreshing its contents).

Now it sounds like you are getting server side event handlers being executed as an apparent result of calling a page method from JS. AFAIAA, invoking a page method using javascript should not cause the page to go through its normal page lifecycle - so Page load, init etc. and these events should not be executing. So that is strange.

Suggestion: -

See Anz's comments and Dave's replies here encosia.

Could it be that you are having similar problems to Anz? i.e. The page method is invoked and but then your page is posting back immediatly after?


This is so because ASP.NET Ajax and ASP.NET Callbacks are two different things and are implemented differently. Unfortunately you have to use both Page.IsAsync and ScriptManager.IsInAsyncPostBack.


Page.IsASync probably returns whether the page was set as Async in the page directive

<%@ Page Language="vb" Async="true" ...

The autopostback flag is so that you don't get a postback after every single control action, so the user can fill in an entire form and then only create the postback and trigger all the related code.

It's not really weirdness, they designed it this way so that the server side code will always be synchronized with the client side. So if you make a drop down list selection on the page and a postback occurs then that drop down list change executes it's own code along with the control that triggered the postback. You may want to read up more on the ASP .Net page lifecycle. it made things much more clear for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号