Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

Ajax Tutorial for Beginners: Part 1

Rate me:
Please Sign up or sign in to vote.
4.67/5 (71 votes)
2 Dec 2008CPOL5 min read 333.2K   4.3K   156  
Ajax Tutorial for Beginners: Part 1
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Ajax Tutorial Part 1</title>
    <script language="javascript" type="text/javascript">
    
		var xmlHttpRequest;
		        
        function GetTime()
        {
			//create XMLHttpRequest object
            xmlHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");           
            
            //If the browser doesn’t support Ajax, exit now
			if (xmlHttpRequest == null)
			return;
			
			//Initiate the XMLHttpRequest object
            xmlHttpRequest.open("GET", "Time.aspx", true);
			
			//Setup the callback function
            xmlHttpRequest.onreadystatechange = StateChange;            
            
            
            //Send the Ajax request to the server with the POST data
            xmlHttpRequest.send(null);
        }
        function StateChange()
        {
			
            if(xmlHttpRequest.readyState == 4)
            {
				document.getElementById('lblTime').value = xmlHttpRequest.responseText;
				          
            }
        }        	
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<input type="text" id="lblTime" />
		<br />
		<input type="button" id="btnGetTime" value="Get Time" onclick="GetTime();" />
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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



Comments and Discussions