ASP. NET 2.0 Client Callback Problems

Source: Internet
Author: User

1. Http://social.msdn.microsoft.com/Forums/en-US/netfxjscript/thread/6c4db554-539a-4b5e-9c0c-e1c41eed4fbb/

Async callusing ASP. NET 2.0 Client Callback won't work properly if the OnComplete function makes another Client Callback

Ref: http://forums.microsoft.com/MSDN/ShowPost.aspx? PostID = 270828 & SiteID = 1

If you make an asynchronous callback using ASP. NET 2.0 client callback and the OnComplete function you defined makes another callback then you will exeperience unwanted behavior. Here is how it gets started...

[Code lang = "javascript"]

Code Snippet

Function StartFirstCallback (){
// Execute first callback. you get this script from Page. ClientScript. GetCallbackEventReference
WebForm_DoCallback ('_ page', "", OnFirstCallbackComplete, null, null, true );
}

Function OntFirstCallbackComplete (result, context ){
Alert ("First Callback Complete ");
// Execute second callback. you get this script from Page. ClientScript. GetCallbackEventReference
WebForm_DoCallback ('_ page', "", OnSecondCallbackComplete, null, null, true );
}

Function OnSecondCallbackComplete (result, context ){
Alert ("Second Callback Complete ");
}

 

[/Code]

When you execute the StartFirstCallback function, you will see your alert winows showing "First Callback Complete" and "Second Callback Complete" messages. the reason for this lies in a minor bug in "WebForm_CallbackComplete" function of inbuilt ASP. NET function. here is the function extracted using Reflector:

[Code lang = "javascript"] Code Snippet

Function WebForm_CallbackComplete (){
For (I = 0; I <_ pendingCallbacks. length; I ++ ){
CallbackObject = _ pendingCallbacks [I];
If (callbackObject & callbackObject. xmlRequest & (callbackObject. xmlRequest. readyState = 4 )){
WebForm_ExecuteCallback (callbackObject );
If (! _ PendingCallbacks [I]. async ){
_ SynchronousCallBackIndex =-1;
}
_ PendingCallbacks [I] = null;
Var callbackFrameID = "_ CALLBACKFRAME" + I;
Var xmlRequestFrame = document. getElementById (callbackFrameID );
If (xmlRequestFrame ){
XmlRequestFrame. parentNode. removeChild (xmlRequestFrame );
}
}
}
}

 

[/Code]

 

The bug is due to the point at which the WebForm_ExecuteCallback function gets invoked. this function is responsible to invoke the OnComplete function. but the "_ pendingCallbacks" "global" array is updated only after executing the OnComplete function. note that "_ pendingCallbacks" global array is also used inside"WebForm_DoCallback"Function to add new requests. The following piece of code is extracted from WebForm_DoCallback function:

[Code lang = "javascript"]

Code Snippet

Function WebForm_DoCallback (...){
...
...
Var callback = new Object ();
Callback. eventCallback = eventCallback;
Callback. context = context;
Callback. errorCallback = errorCallback;
Callback. async = useAsync;
Var callbackIndex = WebForm_FillFirstAvailableSlot (_ pendingCallbacks, callback );
...
...
}

 

 

[/Code]

[/Code]

 

Here is what happens when the StartFirstCallback function is invoked for the first time:

1. WebForm_DoCallback is called
2. _ pendingCallbacks is updated by adding this new request (from step 1). _ pendingCallbacks. length = 1
3. Callback completes and WebForm_CallbackComplete is called.
4. [inside WebForm_CallbackComplete]: walks the _ pendingCallbacks array (which has one element) and executes the OnComplete function viz. OnFirstCallbackComplete.
5. [inside OnFirstCallbackComplete]: "First Callback Complete" message is shown. Another async callback requested (OnComplete = OnSecondCallbackComplete)
5.1 [inside WebForm_DoCallback]: _ pendingCallbacks is updated by adding this new request (from step 1). _ pendingCallbacks. length = 2.

Now by the time _ pendingCallbacks array is updated inside WebForm_CallbackComplete function at step 4, the async operation completes so quickly that WebForm_CallbackComplete is invoked again for second call (made at Step 5 ). here the _ pendingCallbacks has 2 elements and most importantly the 0th element is still available (because "_ pendingCallbacks [I] = null" statement is still not executed for the very first call) which means the OnStartFirstCallbackComplete function is called again. this starts a kind of infinite calland only delayed response can stop this.

FIX:
Fixing this requires just moving the specified statement as the last statement in WebForm_CallbackComplete routine. This ensures that _ pendingCallbacks global array is updated before the OnComplete function gets invoked. Here is the updated version.

[Code lang = "javascript"]

Code Snippet

Function

WebForm_CallbackComplete_SyncFixed (){
// SyncFix: the original version uses "I" as global thereby resulting in javascript errors when "I" is used elsewhere in consuming pages
For (var I = 0; I <_ pendingCallbacks. length; I ++ ){
CallbackObject = _ pendingCallbacks [I];
If (callbackObject & callbackObject. xmlRequest & (callbackObject. xmlRequest. readyState = 4 )){
// The callback shocould be executed after releasing all resources
// Associated with this request.
// Originally if the callback gets executed here and the callback
// Routine makes another ASP. NET ajax request then the pending slots and
// Pending callbacks array gets messed up since the slot is not released
// Before the next ASP. NET request comes.
// FIX: This statement has been moved below
// WebForm_ExecuteCallback (callbackObject );
If (! _ PendingCallbacks [I]. async ){
_ SynchronousCallBackIndex =-1;
}
_ PendingCallbacks [I] = null;

Var callbackFrameID = "_ CALLBACKFRAME" + I;
Var xmlRequestFrame = document. getElementById (callbackFrameID );
If (xmlRequestFrame ){
XmlRequestFrame. parentNode. removeChild (xmlRequestFrame );
}

// SyncFix: the following statement has been moved down from abve;
WebForm_ExecuteCallback (callbackObject );
}
}
}

 

 

[/Code]

 

Lastly, in order to make sure this function is called instead of original version, the following statement shoshould be executed as startup script.

[Code lang = "javascript"]

Code Snippet

If (typeof (WebForm_CallbackComplete) = "function "){
// Set the original version with fixed version
WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
}

 

 

[/Code]

 

[/Code]

 

[/Code]

[/Code]

2. http://forums.asp.net/p/934338/1141715.aspx#1141715

Client CallBack doesn' t support sync operations

GetCallbackEventReference overloaded with this signature:

Public function GetCallbackEventReference (
Control: Control,
Argument: String,
ClientCallback: String,
Context: String,
UseAsync: boolean
): String

But when I try to use callback syncroniously it's still act async way.
To clarify what I have just said little code snippet:

Put an html button on web form make it server-side control.
When in page_load write:
Button1.Attributes ["onclick"] = ClientScript. GetCallbackEventReference (this, "", "callback", null, false) + "; alert (xxx );";

At client-side the code is as following:

 

<Script language = javascript>

 

 

 

 

 

 

Var

Xxx;

 

 

 

Function

Callback (response, context)

{

Xxx =

 

"I shoshould see this message"

;

}

 

 

</Script>

So, if I pass useAsync = false alert (xxx) showould happens after callback event.
But it's not.
If you run this code you 'l see alert "undefined ".

------------------------------

I'm having the same problem. The reason to occur that thing is that the function WebForm_DoCallback (Javascript function generated by the Framework) have the following line of code:

XmlRequest. open ("POST", theForm. action,True);

To do a Syncronous call the third parameter have to be false.

Anyone know if the Microsoft Framework has any function to create a function with a xmlRequest syncronous? Like:

XmlRequest. open ("POST", theForm. action,False);

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.