views:

554

answers:

4

I am working with ASP.net.

I am trying to call a method that exists on the base class for the page I am using. I want to call this method via Javascript and do not require any rendering to be handled by ASP.net.

What would be the easiest way to accomplish this.

I have looked at PageMethods which for some reason are not working and found that a lot of other people have had trouble with them.

+2  A: 

It depends on what the method relies on, but assuming it is a static method or that it does not rely on the Page Lifecycle to work, you could expose a webservice endpoint and hit that with whichever Javascript calling mechanism you would like to use.

Thunder3
+1  A: 

What library are you using to make Ajax calls? If you are using JQuery then you can create static methods and call them on your page. Let me know if you need further help!

azamsharp
A: 

As Thunder3 suggests, expose a Web Service. Once you have done this, you can register the webservice with the ScriptManager (or ScriptManagerProxy), which will cause a JavaScript wrapper to be generated. This wrapper gives you a good interface to the call.

Robert Wagner
A: 

To extend on the point made by @Azam, if you don't want to render the html on the page, you can set the return type to something else such as xml and do a response.write like I have in the code below. During the GET I want to send back the html, but during the POST I send back some XML over the wire.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        If Request.HttpMethod = "GET" Then
            'do some work and return the rendered html
        ElseIf Request.HttpMethod = "POST" Then
            'do some work and return xml
            Response.ContentType = "text/xml"
            Response.Write("<data></data>")
            Response.End()
        Else
            Response.StatusCode = 404
            Response.End()
        End If
    End Sub
Toran Billups