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

Passing parameters after calling code behind function

1 Answer 153 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
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)
03.    var num = parseInt( prompt("Enter an integer number", "1") );
05.    PageMethods.multiply(num, function (response, args){ TheSuccess(response, args); });
08.function TheSuccess(response, args)
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.    }


Here is the code at server side

02.public static int multiply(int num)
04.   int res = num * num;
05.   return res;
08.protected void btnCallback_Click(object sender, EventArgs e)
10.   string extra = "User agreed to do this";
11.   btnCallback.Text = extra;


Any ideas about how to achieve this.

Thanks in advance.


1 Answer, 1 is accepted

Sort by
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 -

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

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.
General Discussions
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question