Question

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?

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top