Click here to Skip to main content
Click here to Skip to main content

Ajax Asynchronous postback

, 6 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Ajax Asynchronous Postback Method

Introduction 

Ajax helps the users to send and receive value from server asynchronously.

Background 

Using ajax we can send and receive data from server with our reloading the page(postback).  So the page will nor refresh if a server call encounters.

Using the code

A jquery library should be added to avail this ajax functionality to the page.

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

Then we can use $.ajax({}); method in our page script.

syntax for this function

$.ajax({name:value, name:value, ... }

The ajax() method is used to perform an AJAX (asynchronous HTTP) request.

All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Click here for more details

usually this method would be like this.

 $.ajax({
                    type: "POST",
                    url: "sample.aspx/ShowValue",
                    data: "{'data':'myname','value1':'123456a'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    success: function (msg) {
                        suc(msg);
                    },
                    error: function (msg) {
                        alert(msg.responseText);
                    }

                });

 

url: "sample.aspx/ShowValue"  specifies the page name and the function name.

    [WebMethod]
    public static string ShowValue(string data, int value1)
    {
        return DateTime.Now.ToString() + " " + data + " " + value1;
    }
the method in the code behind should be a static method.
data: "{'data':'myname','value1':'123456a'}" 

The parameters passed to the code behind as a json object.

A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and put methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object.

 myString = new JSONObject().put("JSON", "Hello, World!").toString();  
produces the string {"JSON": "Hello, World"}.
  

the following blog post contains 5 postback in a single button click without user awareness.

Find the full code


History

Keep a running update of any changes or improvements you've
made here.

License

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

Share

About the Author

Christopher John Paul
Software Developer
India India
I am a software developer with a passion to develop innovative ideas.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMarcus Kramer6-Sep-12 7:11 
GeneralMy vote of 1 PinmemberSelvin6-Sep-12 5:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by Christopher John Paul
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid