views:

36

answers:

1

From this example we can call back server from JavaScript without post back , but i want to make CallServer method to return the result and i try it as below

    string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "function(result) {alert(result);response = result;}", "context", "function(result) {response = 'Err';}", true);
    string callbackScript = "function CallServer(arg, context) { var response; " + callBackReference + "; return response;}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);

and in JavaScript i do the below

response = CallServer("username", "");
 alert("response:" + response);

always response equal undefined although i make it asynchronous ...
Could any help me in finding a solution for such a problem ?

NOTE: My main problem that lead me to try the above solution that my need to call 'CallServer' method in OnClientClick of Asp button and return true or false as below

 <asp:Button ID="Button1" runat="server" Text="Button" 
        OnClientClick="return CallServer('arg','');" onclick="Button1_Click" />
+1  A: 

You can use ASP.NET Page Methods for such problems.
Here is an example.

Also, you can use jQuery to call an ASP.NET Page Methods.

Sure, you need to enable "EnablePageMethods" of ScriptManager to enable PageMethods.

Ahmed
i already use jquery to do that but it have some security issues speically with sharepoint
Space Cracker
What are these security issues? How are they related to jquery/ASP.NET Ajax Methods?
Ahmed