Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Firing the ASP.NET 2.0 Callback Method Right After Registering!!

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.

 

Published Monday, November 05, 2007 4:39 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below