Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#
Tip/Trick

Asychronous Web Service Call

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Feb 2013CPOL 11.5K   4   1
Asynchronous Web Service Call

Introduction

This tip gives us an idea to call methods of page in an asynchronous way.

Background

To call method asynchronously, AJAX has to provide build ins example $.ajax( ).

Using the Code

Step 1

Create a simple AsychCall web page (.aspx). Create a web method, which we need to consume asynchronously like:

C#
[WebMethod]
[ScriptMethod()]
public static bool ExecuteReport(string reportingYear )
{
   
    bool returnvalue = true;
    int reportReturnValue = -1;
    try
    {
        reportReturnValue = GenerateReport.ReportGeneratereportingYear 
        if (reportReturnValue == -1)
        {
            return false;
        }
        return returnvalue;
    }
    catch (System.Exception ex)
    {
        return returnvalue;
    }
}

Step 2

Create another webpage and place a button like:

ASP.NET
<td>
     <asp:Panel ID="pnlReportStatusMsg" 
                runat="server" Visible="true">
         <input type="button" id="btnReportGenerate" 
                    value="Generate Report" onclick="executeReport();"
                        runat="server" />
     </asp:Panel>
</td>

Step 3

On the button click, call a JavaScript method "executeReport(); like:

JavaScript
$.ajax({
       type: "POST",
       url: "/AsychCall.aspx/ExecuteReport",
       data: "{reportingYearKey:'" + FiscalYearValue }",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (msg) {
           if (msg.d == false) {
               document.getElementById('<%= lblReportStatus.ClientID %>').
               style.display = 'block';
               $("#<%=lblReportStatus.ClientID %>").text
               ("Report generation process is not enabled for selected
               reporting fiscal year, therefore please contact administrator
               for the corresponding changes.");
               document.getElementById('<%= btnReportGenerate.ClientID %>').
               disabled = false;
           }
       },
       error: function (request, status, error) {
           alert(request.responseText);
       }
   });

Step 4

Compile this code. You see, when you click the button "Generate Report", AJAX method asynchronously calls Web method "ExecuteReport" from AsychCall Web page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSince this is specifically about async Pin
fjdiewornncalwe15-Feb-13 11:02
professionalfjdiewornncalwe15-Feb-13 11:02 
Because you are specifically talking about sending a call asynchronously, I think you should have been explicit in the code by adding the async: true line in your ajax call so that things are a little clearer to someone new on the topic. That way, they can easily see the difference it makes by simply toggling that value between true and false.
I wasn't, now I am, then I won't be anymore.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.