Using jQuery to directly call ASP.NET AJAX page methods

No.of Views2015
Bookmarked0 times
Downloads 
Votes0
By  sandy060583   On  16 Apr 2010 01:04:53
Tag : JQuery , AJAX
Here I am looking to explain how to call page methods using jQuery.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Here I am looking to explain how to call page methods using jQuery.

Step 1: Define your Page Methods in code behind:

[WebMethod]
    public static int MyMethod1()
    {
        return 13;
    }
    [WebMethod()]
    public static string MyMethod2(string a, int b)
    {
        return a + " –> " + b.ToString();
    }

Step 2: Include the jQuery Library on Default.aspx page:

<script src="Js/jquery.min.js" type="text/javascript"></script>

If you do not have jQuery file in Js folder then use following:

<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">

Step 3: Now Write the code to Call Page method:

<script type="text/javascript">
        function PageMethod(fn, paramArray, successFn, errorFn)
        {
            var pagePath = window.location.pathname;
            //Create list of parameters in the form : {"paramName1":"paramValue1","paramName2":"paramValue2"}
            var paramList = ”;
            if (paramArray.length > 0)
            {
                for (var i=0; i<paramArray.length; i+=2)
                {
                    if (paramList.length > 0)
                        paramList += ‘,’;
                    paramList += ‘"’ + paramArray[i] + ‘":"’ + paramArray[i+1] + ‘"’;
                }
            }
            paramList = ‘{‘ + paramList + ‘}’;
            //Call the page method
            $.ajax({
                type: "POST",
                url: pagePath + "/" + fn,
                contentType: "application/json; charset=utf-8",
                data: paramList,
                dataType: "json",
                success: successFn,
                error: errorFn
            });
        }
        function AjaxSucceeded (result)
        {
            alert(result.d);
            $("#Result").text("Result : " + result.d);
        }
        function AjaxFailed (result)
        {
            alert("Error on Page");
        }
        function CallPageMethod1()
        {
            PageMethod("MyMethod1", [], AjaxSucceeded, AjaxFailed);
            return false;
        }
         function CallPageMethod2()
        {
            PageMethod("MyMethod2",["a", "value", "b", 2], AjaxSucceeded, AjaxFailed);
            return false;
        }
    </script>

Step 4: To Test your code use following html on default.aspx:

<form id="form1" runat="server">
        <h1>
            Using jQuery To Call ASP.NET Page Methods and Web Services</h1>
        <div>
<asp:Button ID="Button1" runat="server" Text="With No Parameter" OnClientClick="return CallPageMethod1();" />
   <asp:Button ID="Button2" runat="server" Text="With Parameter" OnClientClick="return CallPageMethod2();" />
        </div>
        <div id="Result">
        </div>
    </form>

jQuery makes an AJAX call to the MyMethod1 & MyMethod2 page method and replaces the div’s text with its result.

Very Simple!!!

Hope You will also get benefit from this.

Related Post:

http://ramanisandeep.wordpress.com/2009/09/22/calling-web-service-methods-using-asp-net-ajax/

 
Sign Up to vote for this article
 
About Author
 
sandy060583
Occupation-
Company-
Member Type-Fresh
Location-Not Provided
Joined date-03 Jul 2009
Home Page-
Blog Page-
 
 
Other popularSectionarticles
    Here i am going to discuss about on how to clear the file upload control using jQuery. To clear file upload control, you have to place file upload control within the DIV tag like below.
    Published Date : 02/Nov/2010
    In this snippet, I will show how to make all elements in array to UpperCase using JQuery. Recently I have published an article for display subcategories using JQuery AJAX with dropdown list, if you are aware of this, you can read here.
    Published Date : 03/Jan/2011
    In this snippet, I will explain how to disable button after clicked once using JQuery. In the web application we have use the many buttons to do some specific operations.But sometimes users click many times in same button, this is giving unexpected results. So this snippet gives solutions to disable once click button and prevent unexpected result.
    Published Date : 19/Jan/2011
    In this snippet I will show you how to create numeric textbox using JQuery.This snippet is allow to enter numbers,backspace keys only.
    Published Date : 21/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top