C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Hands on AJAX


Posted Date: 19 Feb 2006    Resource Type: Articles    Category: General
Author: Sumit ThomasMember Level: Silver    
Rating: Points: 5



Introduction


AJAX is one of the Buzz words in Web development nowadays, thanks to some cool implementation by Google in gmail.com. Here is a simple example that I tried to start off my first experiment with AJAX.


The Web page



I started off by creating a Web form in ASP.NET.

The source code of the Web page is as follows:


<script language="C#" runat="server">
public void Page_Load(
object sender, EventArgs e )
{
SqlConnection con = new
SqlConnection("server=(local);user id=sa;pwd=;database=northwind");
SqlCommand cmd = new SqlCommand("select * from employees order by LastName",
con);
con.Open();
SqlDataReader dr =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
while( dr.Read() )
{
Response.Write( dr["FirstName"].ToString() + ", " +
dr["LastName"].ToString() + "
");
}
dr.Close();
}
</script>



Save this page as db.aspx

The code is very simple. I am connecting to the Northwind database and displaying the Employee names from the Employees table.

Now the server side page need not be a ASP.NET page, it could be PHP, ASP, JSP or any other server side script.


The Client page



The source code for the client side page is as follows:


<html>
<head>
<script>
var X=null;

//Function to get the XMLHTTP Object. Gets it in a IE/Mozilla friendly way.
function getXMLHTTP(){
var X;
//For IE
try{
X=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
X=new ActiveXObject("Microsoft.XMLHTTP");
} catch(oc){
X=null;
}
}
//For Non-IE browsers
if(!X && typeof XMLHttpRequest !="undefined") {
X=new XMLHttpRequest();
}
return X;
}

//This function is called in the document onLoad event to fill the DIV tag
//with the data from the server side web page.
function FillData(d)
{
//Abort if the XMLHTTP object is already initialised or in use
if(X && X.readyState!=0){
X.abort()
}

X=getXMLHTTP();

d = document.getElementById(d);
eval(d).innerText = "Loading....";

if(X){

//Use the object's open method and pass the required arguments.
//First argument is the method, GET or POST
//Second argument is the web page's URL
//Third argument is to mention if the process should be asynchronous or not
X.open("GET","db.aspx",true);

// This function is called only if we get a complete response from the web page.
X.onreadystatechange=function() {

if(X.readyState==4&&X.responseText) {
// Once we get the complete response, pass the response text to the Div tag.
eval(d).innerHTML = X.responseText;
}
};
}
X.send(null);
}

function Fill()
{
FillData("disp");
}
</script>
</head>
<body>
<div id="disp">
</div
</body>
</html>



Save this page as a HTML file.

Place both the files in web server and if you run the HTML file you should see the data from Employee table populating the DIV tag. This works in the latest version of IE, FireFox and Opera.

Summary



As you can see from this simple example, AJAX is not really a new technology as we are using the age old Javascript functions to implement the same. If you are good at DHTML, it is left to your imagination as to what you can do with it.

Happy Coding!
Sumit




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Implementation Patterns
Previous Resource: MS Code Names 1.0
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use