Using ASP.NET PageMethods with JQuery DataTables?

2019-08-31 04:13发布

问题:

For starters, I'm new to ASP.NET Ajax stuff, and only getting back into JQuery after a long hiatus, so excuse any stupid sounding parts of this question :)

I would like to use the DataTable plug-in for JQuery in my ASP.NET page. Additionally, I'd like to do server-side pagination. It seems like in order to do that, I would have to create a web service and provide the url of said service to the "sAjaxSource" property of the DataTable.

I'm just now discovering "PageMethods", which appear to be similar to a web service but are written directly on a specific page. It would be nice if the DataTable plug-in and the ASP.NET "PageMethods" would work nicely together, but I'm not sure how that would work.

So I guess I'm wondering if the url used for sAjaxSource can be stated in a way that points to the page itself and means "use this PageMethod on this page". Or is there a built in way to manually construct the ajax call/response so I could use something like "PageMethods.GetTable()" directly? Hopefully this makes sense, but please let me know if it is unclear. Thanks in advance!

回答1:

You can call a Page Method on a page in the same way you would a web service, like this

MyPage.aspx/WebMethodName

Just make sure your method is static and has the [ScriptMethod] attribute added.