开发者

Is it possible to get an asp.net 'OnClicked' callback in javascript to call a function in a web control?

It does seem like it wouldn't be possible since callback functions seem to b开发者_开发百科e page specific but in case it's possible I'd like to do that.


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.
        }
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜