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>
精彩评论