views:

19

answers:

1

Title says it all, it does seem like it wouldn't be possible since callback functions seem to be page specific but in case it's possible I'd like to do that.

+1  A: 

You can call static page methods marked with the WebMethod attribute using ASP.NET Ajax if you configure the ScriptManager to do so:

<form id="form" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server"
        EnablePageMethods="true" />
    .
    .
    .
</form>

[WebMethod]
public static int Foo(string bar)
{
    return 42;
}

Then in your client-side code:

function callFoo(bar)
{
    return PageMethods.Foo(bar);
}

You can also do the same with jQuery:

function callFoo(bar)
{
    $.ajax({
        type: "POST",
        url: "YourPage.aspx/Foo",
        data: {
            "bar": bar
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(message) {
            // Do something.
        }
    });
}
Frédéric Hamidi