This is a migrated thread and some comments may be shown as answers.

Passing parameters after calling code behind function

1 Answer 397 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ivo
Top achievements
Rank 1
Ivo asked on 30 Dec 2016, 06:18 AM

Hi everybody, I hope someone of you can give a hand. I will post some sample code however they are pretty much what I am trying to do.

I have a button which executes both client side (OnClientClicking) and server side (OnClick) events. The point is that from the client side I execute a PageMethod in order to get the result of a calculation. Up to this point everything is OK but here comes the tricky part.

I need to check for the result in order to determine if it is greater than some value. If this condition is true then I ask the user to confirm if he wishes to continue, so if the user says YES then the code continues executing OnClick event but if user says NO then OnClick must not be executed.

I have tried passing teleriks args as a parameter but it is not working (OnSuccess function receives it as null). I do this in order to call args.set_cancel() method.

Here is the code at client side

01.function btnCallback_Clicking(sender,args)
02.{
03.    var num = parseInt( prompt("Enter an integer number", "1") );
04.
05.    PageMethods.multiply(num, function (response, args){ TheSuccess(response, args); });
06.}
07.
08.function TheSuccess(response, args)
09.{
10.    //alert(' TheSuccess: \nResult obtained was: ' + response);
11.    if (response > 70) {
12.        var cancel = !confirm("Result obtained was greater than 70 ¿Continue?");
13.        args.set_cancel(cancel);
14.    }
15.}

 

Here is the code at server side

01.[System.Web.Services.WebMethod]
02.public static int multiply(int num)
03.{
04.   int res = num * num;
05.   return res;
06.}
07. 
08.protected void btnCallback_Click(object sender, EventArgs e)
09.{
10.   string extra = "User agreed to do this";
11.   btnCallback.Text = extra;
12.}

 

Any ideas about how to achieve this.

Thanks in advance.

 

1 Answer, 1 is accepted

Sort by
0
Niko
Telerik team
answered on 03 Jan 2017, 12:15 PM

Hello Ivo,

All PageMethods are asynchronous. Thus they do not prevent the JS execution after that and the form is still submitted.

This means that to overcome the issue you could try any of the following approaches.

First option is to avoid using PageMethods.

The second option is to comply with the async nature of the PageMethods. I imagine the following implementation: always cancel the postback of the button, call the PageMethods.multiply and in the success callback execute the logic - you could even submit the form if you need to - form. submit().

The third option is to try and make the PageMethods synchronous. Here is a thread in SO on the topic - http://stackoverflow.com/questions/6700303/any-way-to-do-a-synchronous-pagemethods-call.

Hope this helps. Please, let me know if you need anything else.

Regards,
Niko
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
General Discussions
Asked by
Ivo
Top achievements
Rank 1
Answers by
Niko
Telerik team
Share this question
or