views:

250

answers:

2

I am using ASP.NET Page Methods for my application. Everything works like charm but I do not want my page methods generated inline in the page.

  1. Is there any way to move them to a WebResource.axd file or something similar. I don't really want to write my own proxy just to move the generated one away from the page.

  2. I have multiple page methods in my base page. Is there a way to tell the script manager which methods I want included for the particular page as I am not using all methods on all pages?

A: 

Yes, that is my question too? Anyway to this?

Marko
+2  A: 

I have multiple page methods in my base page. Is there a way to tell the script manager which methods I want included for the particular page as I am not using all methods on all pages?

I'm not sure whether this is possible. What I would do then however is to move your methods that are specific to a certain page in the actual page itself rather than in the base page.

What you could do is to use asmx webservices instead of using page methods for accessing server-side logic from JavaScript.

[System.Web.Script.Services.ScriptService]
public class MyWebService
{

   [WebMethod]
   public string GetData(int id)
   {
      //do some work
      //return result
   }

}

In your aspx or ascx code you do the following

function someFunction(){
   WebServiceNamespace.MyWebService.GetData(123, onSuccessCallback, onErrorCallback);
}

function onSuccessCallback(result){
   //process your result. Usually it is encoded as JSON string
   //Sys.Serialization.JavaScriptSerializer.deserialize(...) can be used for deserializing
}

function onErrorCallback(){
   //display some info
}

You would have to look on how the returning object of your webservice is encoded. Normally it is encoded as Json. I don't remember now whether this has to be specified explicitly in your web.config.

//Edit:
What I forgot. You can use the asp.net ScriptManager for registering your scripts and web services:

<asp:ScriptManager ID="ScriptManager1" runat="server">
   <Scripts>
      <asp:ScriptReference Path="~/scripts/WebServiceScripts.js" />
   </Scripts>
   <Services>
      <asp:ServiceReference Path="~/Services/MyWebService.asmx" />
   </Services>
</asp:ScriptManager>
Juri
The problem is that I need the page methods in controls which are put on the pages based on users' settings. I was hoping to avoid writing proxies because I would need to refactor in two places. If we write too many methods I will move them to a web service. I will also need to handle authentication. PageMethods were perfect for me except that I want them in controls but if I need to I will write a web service.
Stilgar
I had approximately the same problem. I also wanted to have such functionalities on UserControls instead of the page itself. But since it isn't possible, I moved everything to webservices, which actually isn't too bad in the end.
Juri