Tutorial :How do I call a serverside function from javascript?



Question:

I have a javascript function from where I am trying to call the serverside function WebSchedule.Client.RadGrid1_SelectedIndexChanged. This is the code:

<script type="text/javascript">         function RowSelected(sender, args)        {           var dataKeyValue = args.getDataKeyValue("Order_No");           document.getElementById("txtOrderno").value = dataKeyValue;           WebSchedule.Client.RadGrid1_SelectedIndexChanged(sender, args);       }   </script>  

However, I get an error "WebSchedule is undefined". What am I doing wrong?


Solution:1

Check out so called page methods.

Btw, is this supposed to be server side method or client side?

function RowSelected(sender, args) {       var dataKeyValue = args.getDataKeyValue("Order_No");            document.getElementById("txtOrderno").value = dataKeyValue;      WebSchedule.Client.RadGrid1_SelectedIndexChanged(sender, args);   }   

So - seems to me that's a javascript function.

As far as i understand, RadScriptManager inherits from System.Web.UI.ScriptManager. This is what EnablePageMethods prop does: "Gets or sets a value that indicates whether public static page methods in an ASP.NET page can be called from client script."

Those words in bold means - you won't see any control in that serverside method cause of lack of viewstate.

Therefore - consider using Update Panel control (i've got no ideas how it goes together with telerik controls) or make sure that your serverside method is completely stateless (pass needed data through javascript function).

Anyway - seems to me that you should investigate nature of page methods closer.


Solution:2

Your server side pagemethods must have the WebMethod attribute and be declared public static.

Then using javascript they can be called using this syntax: PageMethods.MethodName()


Solution:3

Client =/= Server

Look into Asynchronous Javascript and XML.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »