After exploring various ajax implementations for ASP.NET, I have come to the conclusion that my own implementation is the best of them all... :D
To implement this, simply create a JavaScript function like the one below:
<script type="text/javascript">
function callasync(url, container_id) {
var ajax = false;
if (window.XMLHttpRequest) {
//Modern browsers
ajax = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
//Internet Explorer
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
//Older version
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
else
return false;
if (container_id != '') {
document.getElementById(container_id).innerHTML = "<table width='100%' height='100%'><tr><td style='text-align: center; vertical-align: center;'><a class='LabelInfo'>Wait...</a><br /><img src='Images/Loader.gif' /></td></tr></table>";
}
ajax.onreadystatechange = function () {
handleResponse(ajax, container_id);
}
ajax.open('GET', url, true);
ajax.send(null);
}
function handleResponse(ajax, container_id) {
if (ajax.readyState == 4 && (ajax.status == 200 || window.location.href.indexOf("http") == -1))
if (container_id != '') {
document.getElementById(container_id).innerHTML = ajax.responseText;
adjustLayout();
}
}
</script>
Then, simply call the function "callasync" with a URL and the ID of the containing div element:
callasync('mypage.aspx?QS_KEY=123', 'divId');
In the ASP.NET code-behind, override the Render method as shown below:
protected override void Render(HtmlTextWriter writer)
{
if (Request.QueryString["QS_KEY"] != null)
{
//render your desired content here
}
else
{
base.Render(writer);
}
}