A lead to a great posting Rasmus’ 30 second Ajax tutorial was too good a treat not to share around. Sometimes we all want something very simple to build a thorough understanding of the mechanics of a new technique before we dive into the deeper water beyond. Now, if you are into ASP.NET and not PHP you might like to take a look at my version of this ultra-simple introduction to Ajax with sincere thanks to the original author. Now most simple introductions to Ajax using ASP.NET seem to quickly become complex - introducing libraries and DLLs and helper functions just about as soon as the first paragraph is over. This is because they all seem to miss out the fact that you have to work around the ASP.Net functionality to get thing working in a nice simple manner. I will try and take you through the steps that explain the problem and thus make the solutions seem much more obvious.
So let’s get going. Create a new Web project and, to keep things very simple at this initial stage, add a couple of HTML controls - a text field and a button to the initial page. Note, not regular ASP.NET web forms controls - click the HTML tab.
Then add a JavaScript file to the project and paste the following into it:
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == “Microsoft Internet Explorer”){
ro = new ActiveXObject(”Microsoft.XMLHTTP”);
}else{
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRequestObject();
function sndReq(action) {
http.open(’POST’, ‘WebForm2.aspx?action=’+action);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
alert(response);
document.getElementById(’MessBox’).value = response;
}
}
You will need to add a reference to this JavaScript file to the web page - although you could just add the script to that page directly.