Sunday, October 16, 2011

ICallBackEventHandler Mystery

Hello,

I was working on a project in which I needed to use call server-side code to get data after the page load.
Instead of the latest jquery ajax I preferred to use the old sword for ajax call ICallBackEventHandler.
On reading certain history on Ajax Calls to server, I found that there are many techniques which can help me. But after exploring more about ICallBackEventHandler I thought that will suffice my purpose and the amount of data travelling on wire is also less in this as it uses raw ajax. Some MVP's says that ICallBackEventHandler was a failure, they suggest to use ASP.NET Ajax instead.
But I already had a POC which was giving me expected results. So I didn't bothered to explore that front much.

The requirement was, there were two controls in my page which needed to show data after the page load.
I made two CallServer methods, binding each control.

I tried to call server methods on a button click.

function ServerMethods() {
   CallServer1();
   CallServer2();
}
And the load methods as:

function CheckforContent1() {
    alert('function 1');
}

function CheckforContent2() {
    alert('function 2');
}
On debugging both the functions are called, but at the time of response only the latter CallServer method's CheckforContent method is executed. In my case only CheckforContent2 method gives the alert. Not the first one. It got me confused that why is that happening.

As the deadline was closer I found a workaround for the problem but the original issue kept bugging me.
Workaround:

function ServerMethods() {  CallServer1();  }

    function CheckforContent1() {
    alert('function 1');
    CallServer2();
}
The workaround solved the problem the page to become idle was increased as the second server call was after the first get completed instead of being in parallel which I expected. I kept searching for the answer but couldn't find one. All I can get was the way to do it is the way I did it. When ICallBackEventHandler was designed it was taken into consideration of multiple simultaneous callback's. Since I only needed small data in string format so this served the purpose this time but now I think I may need to explore better options for calling server asynchronously.

No comments: