Ajax sample code for “Hello World” application

Hi ,
Today i am going to explain a simple “Hello World ” application  in asp.net , which will give you a basic idea about using ajax in your application .

(1)Create a asp.net web site in VS200*
(2)On the Page1.aspx , add the following code



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(‘GET’, ‘Page2.aspx);
http.onreadystatechange = handleResponse;

function handleResponse() {
if (http.readyState == 4) {
var response = http.responseText;
// alert(response);
document.getElementById(‘MessBox’).value = response;


(3)Remove all the code from Page2.aspx and in Page2.aspx.cs, Page_Load add following code

protected void Page_Load(object sender, EventArgs e)
Response.Write(“I am coming from Page2 via Ajax call.”);

(4)Set page1 as startup page and click on button.
(5) Value dispalyed in text box is coming via ajax call.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s