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

Server side event doesn't get called if Ajax and OnClientclick added

5 Answers 233 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Shubha Singh
Top achievements
Rank 1
Shubha Singh asked on 29 Oct 2009, 09:15 AM

Below is my scenario:

Scenario:

1. I have one telerik:RadAjaxManager which has telerik:AjaxSettings. In it, AjaxControlID is a button Id and AjaxUpdatedControl is a table which contains that button along with few textboxes, requiredfieldvalidators for them and one server side button.

2. The button has onclientclick and onclick events registered.

3. Onclientclick event calls a simple javascript function which does some validation of form data and returns true or false. (OnClientClick="return ValidatePage();")

4. Onclick event calls some server side implementation.

 Behavior:

1. When a button is clicked,  requiredfieldvalidators (only checks if data is entered) gets fired.

2. When data is entered in the textboxes, javascript function 'ValidatePage()' (checks if VALID data is entered) gets fired.

3. When ValidatePage() returns false, server side event doesn't get fired.(Which is desired behavior)

4. When ValidatePage() returns true, still server side event doesn't get fired.(Which is not desired behavior)

5. When OnclientClick event registration is removed, the server side event gets fired through AJAX after entering data in the textboxes.

6. When button is moved out of the table, both, the javascript function and the server side event gets fired but normal postback occurs.

Desired behavior:

1. First requiredfieldvalidators should get fired.

2. Then 'ValidatePage()' should get fired. If it returns false, it should restrict server side call. If it returns true, server side event should get called through AJAX.

Please let me know if you have any comments on it.

Regards,

Shubha

5 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 02 Nov 2009, 03:14 PM
Hello Shubha,

RadAjax changes submit behavior of the buttons to postback through script/__doPostBack../.
Due to this fact all the script that you append OnClientClick it is inserted right before submit script.
In this case if you have return in front of the submit script it will not be executed regardless whether your return type is true or false. I.e. the following wont cause the alert to be called: onclick="return true;alert(1);"

To workaround this behavior you should construct the script as follow:
1.<asp:Button runat="server" ID="Button1" 
2.        OnClientClick="if(!CallYourFunction()) { return false; };"/>


Regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Shubha Singh
Top achievements
Rank 1
answered on 06 Nov 2009, 11:30 AM
Hi Nikolay,

Thanks for the reply. By applying solution you suggested, the server side method does get called properly but this fix spawns another problem. There are few validators and validationSummary control inside my RadAjaxPanel which has ShowMessageBox="true". This error messagebox pops up twice when the validator fails on button click attempt. I tried EnableClientScript="false" but still it pops up twice.
Is there any way to prevent this?

Regards,

Shubha
0
Shubha Singh
Top achievements
Rank 1
answered on 06 Nov 2009, 12:13 PM
My bad. I was calling Page_ClientValidate('bookingValidationGroup'); through my javascript function. :P. It's working fine now.
Sorry for the inconvenience.
0
Mahesh
Top achievements
Rank 1
answered on 05 Feb 2013, 05:40 AM
i have same problem and using this post i able to call server side function but i got error during execution code
on my save button i m doing
1. save data in database
2. Reset Controls
3. load updated controls
4. set focus on my first control

my error message is

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.<br>Details: Error parsing near 'older1_txtGroupName|<script type="text/j'.
0
Kostadin
Telerik team
answered on 07 Feb 2013, 02:52 PM
Hello Mahesh,

Could you please elaborate a little bit more on your scenario? As far as I can see you have a parse error in your code behind. Sending us your code declaration with the related code behind will help us to pinpoint the reason for that behavior.

Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Ajax
Asked by
Shubha Singh
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Shubha Singh
Top achievements
Rank 1
Mahesh
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or