NOTE: This is designed for v1.0.61025.0 of AJAXExtensionsToolbox.dll
If you've ever returned data from a web service using MS AJAX this technique will look familar.
Call the method from javascript by append PageMethods. to the front of the method name. Add a delegate to the end of the method's parameters to get the result.
<script language="javascript" type="text/javascript"> function MyMethod_Result(ResultString) { alert(ResultString); } function CallMyMethod(){ PageMethods.MyMethod("World", MyMethod_Result); } </script>
I have made the source to this available to download, otherwise take a look at the full explanation below:
Full explanation - Step 1 - Enable Page Methods on your ScriptManager
Set the EnablePageMethods attribute to true
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" /> Step 2 - Mark your method as static and give it the WebMethod attribute
The method has to be declared static. It must also be marked with the WebMethod attribute. You'll probably find that you need to include System.Web.Services
using System.Web.Services;
[WebMethod] public static string MyMethod(string name) { return "Hello " + name; } Step 3 - Call it from Javascript
Call the method from javascript by append PageMethods. to the front of the method name. Add a delegate to the end of the method's parameters to get the result.
<script language="javascript" type="text/javascript"> function MyMethod_Result(ResultString) { alert(ResultString); } function CallMyMethod(){ PageMethods.MyMethod("World", MyMethod_Result); } </script> |
2012年9月19日 星期三
Returning a value from a PageMethod
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言