.NET AJAX callbacks to ASPX Pages

AID: 2575
  • Status: Published

3910 points

  • Bymasterpass
  • TypeTutorial
  • Posted on2010-02-28 at 22:18:37

ASP.NET AJAX callbacks to web service are becoming very popular and unavoidable these days. The feel that the user gets when only a part of the page is updated without even hindering the UI is the advantage that the callbacks offer. The wait for the full page to be posted back and results to appear is reduced thereby keeping a rich user experience. In some situations you would not want to expose the arguments and the methods it supports to the public through the ASMX file. This will require you to move the logic to specific ASPX files or inside the inherited base class. Specifically, this is the most important reason which, I feel, would require you to move web methods to ASPX pages.

Both ASP.NET AJAX Extensions and ASP.NET 3.5 support AJAX callbacks to the web methods placed in ASPX pages or the inherited base class. The following are the steps involved, and we will use a fictitious routine "GetEmployees" to exemplify what we are doing.

The first step is to expose GetEmployees as a web method.

 

1: public partial class Employees : System.Web.UI.Page
2: {
3: [WebMethod]
4: [ScriptMethod]
5: public static List<Employees> GetEmployees(string dept)
6: {
7:   // logic to get the Employee list
8: }

                                  
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window



Important points to be noted :

1. The method needs to be decorated with [WebMethod]
2. The method needs to be decorated with [ScriptMethod] to enable callback
3. The method MUST be static

Go to ScriptManager tag and set the ScriptManager.EnablePageMethods property to true. This will make all the web methods on this page visible to the JavaScript client. Without enabling the EnablePageMethods property the webmethods will not be available in the client side. So always pay a keen attention to enable it.

 

1: <asp:ScriptManager ID=”ScriptManager1” runat=”server” EnablePageMethods=”true” />

                                  
1:

Select allOpen in new window



Finally, Change the JavaScript code to call the method through the PageMethods object instead of the Web Service.The transport of data to the client side can be in XML, JSON, String etc. I would recommend using JSON because it is light weight, fast and can be implemented with ease. When I say transport of data it is actually the result from the GetEmployees() function which we have marked as the WebMethod. when using JSON also pay a little care to the eval() which is known to cause XSS issues.

 

1: function GetEmployees ()
2: {
3:     PageMethods. GetEmployees(dept ,OnSucceeded);
4: }
5:
6: function OnSucceeded (result)
7: {
8:     // logic to display the result
9: }

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window



The Original article was published in my site . Happy coding :)

Asked On
2010-02-28 at 22:18:37ID2575
Tags

Pagemethods

,

Webmethods

,

ajax callbacks

Topic

Asynchronous Javascript and XML (AJAX)

Views
2036

Comments

Expert Comment

by: doramail05 on 2010-04-02 at 16:45:43ID: 12594

when placing webmethod script in a webpage, this webpage has to host in iis in a separate virtual directory
OR just add new item (Web Service) within the project?

Author Comment

by: masterpass on 2010-04-03 at 10:01:46ID: 12621

No need to add a new item to your project if you are going with pageMethods .. you can just add this in the code behind of the aspx file

Add your Comment

Please Sign up or Log in to comment on this article.

Loading Advertisement...

Top AJAX Experts

  1. chaituu

    35,000

    0 points yesterday

    Profile
    Rank: Sage
  2. leakim971

    32,800

    0 points yesterday

    Profile
    Rank: Genius
  3. tommyBoy

    20,000

    0 points yesterday

    Profile
    Rank: Sage
  4. ddayx10

    17,418

    0 points yesterday

    Profile
    Rank: Sage
  5. StingRaY

    15,818

    2,000 points yesterday

    Profile
    Rank: Guru
  6. nap0leon

    15,568

    0 points yesterday

    Profile
    Rank: Wizard
  7. COBOLdinosaur

    10,568

    0 points yesterday

    Profile
    Rank: Genius
  8. maeltar

    8,596

    0 points yesterday

    Profile
    Rank: Guru
  9. mrh14852

    8,000

    0 points yesterday

    Profile
    Rank: Master
  10. mplungjan

    7,600

    0 points yesterday

    Profile
    Rank: Genius
  11. HainKurt

    6,800

    0 points yesterday

    Profile
    Rank: Genius
  12. Ray_Paseur

    4,536

    0 points yesterday

    Profile
    Rank: Savant
  13. hielo

    4,400

    0 points yesterday

    Profile
    Rank: Savant
  14. sudaraka

    4,300

    1,500 points yesterday

    Profile
    Rank: Sage
  15. Eyal

    4,000

    0 points yesterday

    Profile
    Rank: Wizard
  16. zappafan2k2

    4,000

    0 points yesterday

    Profile
    Rank: Guru
  17. GwynforWeb

    4,000

    0 points yesterday

    Profile
    Rank: Genius
  18. gnoon

    4,000

    0 points yesterday

    Profile
    Rank: Sage
  19. AlbertVanHalen

    4,000

    0 points yesterday

    Profile
    Rank: Sage
  20. santhimurthyd

    4,000

    0 points yesterday

    Profile
    Rank: Guru
  21. alex_code

    4,000

    0 points yesterday

    Profile
    Rank: Guru
  22. CodeCruiser

    4,000

    0 points yesterday

    Profile
    Rank: Genius
  23. amar_bardoliwala

    4,000

    0 points yesterday

    Profile
    Rank: Master
  24. techChallenger1

    4,000

    0 points yesterday

    Profile
    Rank: Guru
  25. sonawanekiran

    3,996

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame