开发者

How to do this trick in 'CallServer' method in Implementing Client Callbacks Programmatically Without Postbacks?

开发者 https://www.devze.com 2023-01-04 13:36 出处:网络
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

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; " + callBackRefere开发者_StackOverflownce + "; 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" />


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.


HandleResult will recieve the results from GetCallbackResult for further processing. Add the below code to your aspx page

    <script type="text/javascript">

    //the parameter arg is what is returned from GetCallbackResult
    function HandleResult(arg, context) {
        alert(arg);
    }

</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消