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.