locked
ASP.net Ajax Page Methods RRS feed

  • Question

  • User1891589036 posted

    This seems like a good feature to call server side code from Jquery ajax for partial page update. No viewstate is involved and if there are small number of controls on the page then this could possibly replace the submit/save functionality and avoid a postback. I am just curious why this feature isn't that popular. Updatepanels have bloated viewstate, callbacks send in viewstate to the server as well, and webservices are generic. We could have some form of check for __EventValidation like checking the selected value of the dropdown against a lookup(that populated the dropdown initially) on the server. I am wondering if people are making use of this feature and what their experience is. Its kind of wierd that all the books just talk briefly about it. Is there anything i'm missing?? Thanks 

    Tuesday, July 19, 2011 6:58 PM

Answers

  • User-654786183 posted

    I agree with this partially.  With plain javascript it is little bit difficult to write the code for calling webmethods, callback methods and processing the success/error responses.  But with jQuery and its support to JSON and other various response types make lot of people using this as a combination to call Page methods.  One more point is with ASP.NET MVC this kind of calling Page methods with jQuery and get the response as JSON is widely used by most of the people now and jQuery is built in with ASP.NET MVC.  

    This kind of approach can be applicable to some of the following scenarios mostly.  And we can find lot of examples in the internet too.

    1. Fire & Forgot mechanism (Send notification, Update database for delete/edit/insert, Trigger a webservice)
    2. Use with Lightbox
    3. Dynamically update some area in the web page
    It cannot be applicable for some scenarios
    1. When the page needs to be reloaded after the operation in order to keep the page updated
    2. Pass huge amount of data across layers
    So based on our need, we can choose page methods as an option.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2011 10:15 PM

All replies

  • User-1322853079 posted

    I agree with you.., this feature is not that popular.., but you can go ahead use it..

    my guess.., its not popular because..the page methods has some restrictions..like they should be static...., making it static method..do not allow to access other non-static objects..in the class

    Tuesday, July 19, 2011 10:01 PM
  • User-654786183 posted

    I agree with this partially.  With plain javascript it is little bit difficult to write the code for calling webmethods, callback methods and processing the success/error responses.  But with jQuery and its support to JSON and other various response types make lot of people using this as a combination to call Page methods.  One more point is with ASP.NET MVC this kind of calling Page methods with jQuery and get the response as JSON is widely used by most of the people now and jQuery is built in with ASP.NET MVC.  

    This kind of approach can be applicable to some of the following scenarios mostly.  And we can find lot of examples in the internet too.

    1. Fire & Forgot mechanism (Send notification, Update database for delete/edit/insert, Trigger a webservice)
    2. Use with Lightbox
    3. Dynamically update some area in the web page
    It cannot be applicable for some scenarios
    1. When the page needs to be reloaded after the operation in order to keep the page updated
    2. Pass huge amount of data across layers
    So based on our need, we can choose page methods as an option.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2011 10:15 PM
  • User-1020879250 posted

    There are 2 other things/drawbacks with PageMethods, both on server side.

    1) Exception handling. Since this is not a regular postback, the global.asax/Application_Error block does NOT get invoked.

    So in the static method, the complete code needs to be inside a try-catch & you need to call your own custom error logging mechanism.

    Also take care , that the real exception does not hit the client.

    2) Another poor thing is server-side Validations -- when you submit a form using PageMethods. You need to write all validation code , similar to standard validators yourself. Plus any plain text needs to be under HtmlEncode call.

    Other than the above 2 points, for small little tasks like updating few controls, Page Method works great.

    Remember to verify all input arguments in the static methods -- as they can be manipulated at the client end.

    Wednesday, August 3, 2011 9:59 AM