Click here to Skip to main content
15,881,380 members
Articles / Web Development / HTML

AJAX Demystified - Part Two - The AJAX DataGrid

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
21 Jun 2007CPOL6 min read 82.5K   687   83  
Create an AJAX DataGrid that binds, sorts and pages with no post-backs
var obj;

// Creates the XMLHTTP Request object
function getXMLHTTPRequest()
{
	var xRequest=null;
	if (window.XMLHttpRequest)
	{
		xRequest = new XMLHttpRequest();
	}
	else if (typeof ActiveXObject != "undefined")
	{
		xRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xRequest;
}

// Performs a GET to our processSQL.aspx web page with the query
function sendRequest()
{
	obj=getXMLHTTPRequest();
	if (obj!=null)
	{
	    var query = document.getElementById("txtQuery").value;
	    var rows = document.getElementById("txtRows").value;
		obj.onreadystatechange = processResponse;
		obj.open("GET","http://localhost/ajaxDG/processSQL.aspx?query="+escape(query)+"&rows="+rows,true);
		obj.send(null);
	}
	return false;
}

// Process the HTTP Response when it is complete
function processResponse()
{
	if (obj.readyState == 4)
	{
		if (obj.status == 200)
		{
			var retval=obj.responseText;
			if (document.getElementById("results")!=null)
			{
				document.getElementById("results").innerHTML = retval;
			}
			else
			{
				alert("Error retrieving data!");
			}
		}
	}
}

// Send the query and parameters to the server side script for processing
function generateGrid()
{
	obj=getXMLHTTPRequest();
	if (obj!=null)
	{
	    var query = document.getElementById("txtQuery").value;
	    var rows = document.getElementById("txtRows").value;
	    var sortExpr = document.getElementById("txtSort").value;
	    var page = document.getElementById("txtPage").value;
		obj.onreadystatechange = processResponse;
		obj.open("GET","http://localhost/ajaxDG/processSQL.aspx?query="+escape(query)+"&rows="+rows+"&gridpage="+page+"&expression="+sortExpr,true);
		obj.send(null);
	}
	return false;
}

// Called when a column header is clicked
function sortGrid(sortExpr)
{
	document.getElementById("txtSort").value = sortExpr;
	generateGrid();
}

// Called when the grid is paged
function pageGrid(page)
{
	document.getElementById("txtPage").value = page;
	generateGrid();
}

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)


Written By
Software Developer (Senior) CentralReach
United States United States
I have worked professionally in IT since 2004, and as a software architect since 2008, specializing in user interface design and experience, something I am still extremely passionate about. In 2013 I moved into management, and since then I've held positions as Director of Product Development, Director of Engineering, and Practice Director.

Comments and Discussions