Click here to Skip to main content
15,884,298 members
Articles / Web Development / IIS

Implement Script Callback Framework in ASP.NET 1.x

Rate me:
Please Sign up or sign in to vote.
4.84/5 (56 votes)
2 Aug 20048 min read 469.4K   4.2K   122  
It allows calls to server events from client script code without causing the page to post back and refresh.
var __callbackList = new Array();	//	Holds the asynchronous callback handler

//	Add the Callback Handler to array list
function addToCallbackList(cb) {
	__callbackList[__callbackList.length] = cb;
}

//	Is Microsoft Browser?
var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
var pageUrl = "";			//	Post Back URL
var __theFormPostData = "";	//	Form Data
function WebForm_InitClientCallback(cbUrl) {
	pageUrl = cbUrl;
	var theForm = document.forms[0];	//	ASP.NET currently support single form PostBack only
    count = theForm.elements.length;
    var element;
    re = new RegExp("\\x2B", "g");
    for (i = 0; i < count; i++) {
        element = theForm.elements[i];
         if (element.tagName.toLowerCase() == "input") {
            __theFormPostData += element.name + "=" + element.value.replace(re, "%2B") + "&";
         }
         else if (element.tagName.toLowerCase() == "select") {
            selectCount = element.children.length;
            for (j = 0; j < selectCount; j++) {
                selectChild = element.children[j];
                if ((selectChild.tagName.toLowerCase() == "option") && (selectChild.selected == true)) {
                    __theFormPostData += element.name + "=" + selectChild.value.replace(re, "%2B") + "&";                
                }                
            }
         }
    }
}


//	Callback asynchronously
function WebForm_DoAsyncCallback(eventTarget, eventArgument, eventCallback, context, errorCallback) {
    re = new RegExp("\\x2B", "g");
    if (__nonMSDOMBrowser) {
        var xmlRequest = new XMLHttpRequest();
        postData = __theFormPostData +
                   "__SCRIPTCALLBACKID=" + eventTarget +
                   "&__SCRIPTCALLBACKPARAM=" + escape(eventArgument).replace(re, "%2B");
        if (pageUrl.indexOf("?") != -1) {
            xmlRequest.open("GET", pageUrl + "&" + postData, false);
        }
        else {
            xmlRequest.open("GET", pageUrl + "?" + postData, false);
        }    
        xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlRequest.send(null);
        try
        {
			response = xmlRequest.responseText;
			status   = xmlRequest.getResponseHeader("__SCRIPTCALLBACKSTATUS");
			if (status == "200") 
			{			
				if (eventCallback != null)
					eventCallback(response, context);
			}
			else
			{
				if (errorCallback != null)
					errorCallback(response, context);
				else
				{
					//	For Testing Purposes
					//alert(response);
				}
			}
        }
        catch(e)
        {
            if (errorCallback != null)
                errorCallback(e.message, context);
			else
			{
				//	For Testing Purposes
				//alert(e.message);
			}
        }
    }
    else {
        var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        xmlRequest.onreadystatechange = WebForm_OnClientCallbackComplete;
        var __callbackObject = new Object();
        __callbackObject.xmlRequest = xmlRequest;
		__callbackObject.eventTarget = eventTarget;
		__callbackObject.eventArgument = eventArgument;
        __callbackObject.eventCallback = eventCallback;
        __callbackObject.context = context;
        __callbackObject.errorCallback = errorCallback;
        addToCallbackList(__callbackObject);
        
        postData = __theFormPostData +
                   "__SCRIPTCALLBACKID=" + eventTarget +
                   "&__SCRIPTCALLBACKPARAM=" + escape(eventArgument).replace(re, "%2B");
        usePost = false;
        if (pageUrl.length + postData.length + 1 > 2067) {
            usePost = true;
        }
        if (usePost) {
            xmlRequest.open("POST", pageUrl, true);
            xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequest.send(postData);
        }
        else {            
            if (pageUrl.indexOf("?") != -1) {
                xmlRequest.open("GET", pageUrl + "&" + postData, true);
            }
            else {
                xmlRequest.open("GET", pageUrl + "?" + postData, true);
            }
            xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequest.send();
        }
    }
}


//	Callback Synchronously
function WebForm_DoSyncCallback(eventTarget, eventArgument, eventCallback, context, errorCallback) {
    if (__nonMSDOMBrowser) 
    {
		re = new RegExp("\\x2B", "g");
		var xmlRequest = new XMLHttpRequest();
		postData = __theFormPostData +
					"__SCRIPTCALLBACKID=" + eventTarget +
					"&__SCRIPTCALLBACKPARAM=" + escape(eventArgument).replace(re, "%2B");
		if (pageUrl.indexOf("?") != -1) {
			xmlRequest.open("GET", pageUrl + "&" + postData, false);
		}
		else {
			xmlRequest.open("GET", pageUrl + "?" + postData, false);
		}    
		xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlRequest.send(null);
		try
		{
			response = xmlRequest.responseText;
			status   = xmlRequest.getResponseHeader("__SCRIPTCALLBACKSTATUS");
			if (status == "200") 
			{			
				if (eventCallback != null)
					eventCallback(response, context);
			}
			else
			{
				if (errorCallback != null)
					errorCallback(response, context);
				else
				{
					//	For Testing Purposes
					//alert(response);
				}
			}
		}
		catch(e)
		{
			if (errorCallback != null)
				errorCallback(e.message, context);
			else
			{
				//	For Testing Purposes
				//alert(e.message);
			}
		}
	}
	else
	{
        var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        postData = __theFormPostData +
                   "__SCRIPTCALLBACKID=" + eventTarget +
                   "&__SCRIPTCALLBACKPARAM=" + escape(eventArgument).replace(re, "%2B");
        usePost = false;
        if (pageUrl.length + postData.length + 1 > 2067) {
            usePost = true;
        }
        if (usePost) {
            xmlRequest.open("POST", pageUrl, false);
            xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequest.send(postData);
        }
        else 
        {            
            if (pageUrl.indexOf("?") != -1) {
                xmlRequest.open("GET", pageUrl + "&" + postData, false);
            }
            else {
                xmlRequest.open("GET", pageUrl + "?" + postData, false);
            }
            xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlRequest.send();
        }
        
        try
        {
			response = xmlRequest.responseText;
			status   = xmlRequest.getResponseHeader("__SCRIPTCALLBACKSTATUS");
			if (status == "200") 
			{			
				if (eventCallback != null)
					eventCallback(response, context);
			}
			else
			{
				if (errorCallback != null)
					errorCallback(response, context);
				else
				{
					//	For Testing Purposes
					//alert(response);
				}
			}
        }
        catch(e)
        {
            if (errorCallback != null)
                errorCallback(e.message, context);
			else
			{
				//	For Testing Purposes
				//alert(e.message);
			}
        }        
	}
}


//	Asynchronous Callback Completed
function WebForm_OnClientCallbackComplete() 
{
	for(var i = 0; i < __callbackList.length; i++)
	{
		var __cbObject = __callbackList[i];
		
		if (__cbObject != null && __cbObject.xmlRequest.readyState == 4) 
		{
			try
			{
				xmlText	 = __cbObject.xmlRequest.responseXML;
				response = __cbObject.xmlRequest.responseText;
				status   = __cbObject.xmlRequest.getResponseHeader("__SCRIPTCALLBACKSTATUS");
				if (status == "200") 
				{
					if (__cbObject.eventCallback != null)
						__cbObject.eventCallback(response, __cbObject.context);
				}
				else
				{
					if (__cbObject.errorCallback != null)
						__cbObject.errorCallback(response, __cbObject.context);
					else
					{
						//	For Testing Purposes
						//alert(response);
					}
				}
			}
			catch(e)
			{
				if (__cbObject.errorCallback != null)
					__cbObject.errorCallback(e.message, __cbObject.context);
				else
				{
					//	For Testing Purposes
					//alert(e.message);
				}
			}
			finally
			{
				__cbObject.xmlRequest = null;
				__cbObject = null;
				__callbackList[i] = null;
			}
		}
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Singapore Singapore
Elvin Cheng is currently living in Woodlands, Singapore. He has been developing applications with the .NET Framework, using C# and ASP.NET since October 2002. Elvin specializes in building Real-time monitoring and tracking information system for Semi-conductor manufacturing industry. During his spare time, he enjoys reading books, watching movie and gym.

Comments and Discussions