Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

When I was creating the Polling module I need to fire the server method right after registering the callback method. I thought this would be simple as I just needed to call the method right after registering. Well, not really let's check out the code below:

private void RegisterCallbacks()

{

string sbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");

string cbScript = String.Empty;

// check if the script is already registered or not

if (!Page.ClientScript.IsClientScriptBlockRegistered("CallServer"))

{

cbScript = @" function CallServer(arg,context) { " + sbReference + "} makeServerCall(); ";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);

}

}

In the code above I am firing the method "makeServerCall" right after registering the callback method. The method makeServerCall simply calls the "CallServer" method which should trigger the callback. Unfortunately, for some reason this does not happen.

<

head runat="server">

<title>Untitled Page</title>

<

script language="javascript" type="text/javascript">

function

ReceiveServerData(response)

{

alert(response);

}

function

makeServerCall()

{

 

CallServer('','');

}

</

script>

</

head>

The CallServer method never gets fired. Amazingly, if I simply put a alert inside the "makeServerCall" then the alert message displays correctly.

To solve this problem I used the window.setTimeout function as shown below:

private void RegisterCallbacks()

{

string sbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");

string cbScript = String.Empty;

// check if the script is already registered or not

if (!Page.ClientScript.IsClientScriptBlockRegistered("CallServer"))

{

cbScript = @" function CallServer(arg,context) { " + sbReference + "} window.setTimeout(makeServerCall,100); ";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);

}

}

And now it works correctly!

I am not sure what causes this problem yet! Feel free to drop your comments and resolve this issue.

 

Posted on Monday, November 5, 2007 9:40 AM | Back to top


Comments on this post: Firing the ASP.NET 2.0 Callback Method Right After Registering!!

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net