Script CallBack In ASP.NET 1.1

Introduction

In my last article, Remote Scripting, Webservice Behavior, and Script Callback, I talked about remote scripting, Web service behavior, and script callback in ASP.NET 2.0. However, I didn’t discuss the possibility of script callback in ASP.NET 1.1. So, in this article you will explore the scriptcall back functionality in ASP NET version 1.1. However, for those who yet don’t know what script callback is, I will give a quick overview before proceeding to the implementation details of the same in ASP.NET 1.1.

Script callback, as its name suggests, is the technique/method that calls the server from the client script and remembers the keyword callback that further justifies the name because the way the script calls the server is not a postback but a callback. The same is not postback but callback due to the fact that in postback, the entire page get submitted; in callback, the client script sends a hidden request in the form of DOM’s xmlhttp made to the specific server method (may be a remote page or control). The server, upon recognizing this callback, does not process the entire page but only processes the required method and sends the result back to the client. (Note that the init, load, and prerender of the server page still fire, though.) Thus, callback is faster and because the request is hidden from the client script, your page is not refreshed. When the server returns the result, the client receives the same and, using a DHTML feature, mixes the result contents with the current content of the page.

Now that I have given a quick overview of script callback, let me go through how to implement the same in ASP.NET 1.1 .In ASP.NET version 2.0, there is built-in support for script callback in many controls and there are an interface and class defined for the same. However, in ASP.NET 1.1 to implement the same, you need to define those on your own. Before proceeding to the example, you need to know what all is required to implement the same in ASP.NET 1.1. Below are your requirements for script callback in ASP.NET 1.1.

What Is Needed for the Callback Mechanism

  1. A client script that will raise the callback.
  2. A callback handler (the server page or control—may be remote or on the same page as the client—to handle the callback and send the processed out put for the callback).
  3. A client script that received the callback response from the callback handler.
  4. a callback error handler (optional).

The important point you may ask here is how you could raise the callback from that client script that you send to your data from the client to the server. Here comes microsoft.xmlhttp to come to rescue .The xmlhttp exposes a API using the same you can send, post, and receivethe (may be XML, HTML, or binary).

The syntax is simple yet very useful:

//GET
var xmlRequestobj = new ActiveXObject("Microsoft.XMLHTTP");
xmlRequestobj.Open ("GET", "http://www.codeguru.com/", False);
xmlRequestobj.Send();
response.Write(xmlRequestobj.responseText);


//SEND
var xmlRequestobj = new ActiveXObject("Microsoft.XMLHTTP");
obj = Server.CreateObject("Microsoft.XMLHTTP")
obj.Open ("POST", "http://www.shreeman.com", False)
obj.Send ("data to select");

Now that you know what you need to work with your sample, proceed to implement the same step by step:

How to Implement the Script

You will validate the userid entered by the user from your backend employee table. Although in actual practice, an example could be anything,I found this demonstration quite easy and better to understand. You have a textbox on which events change; you are going to make a hidden call to the server method to get the same validated from the backend.

Client script to raise the callback

Here is the client script that will be generated upon the user. Press the Tab key after entering a value in the UserID textbox:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack && !IsCallback())
{
Label1.Text = "Enter ID & press tab to validate from BE thru
               callback";

}
string callbackRef = "HandleCallBack()";
TextBox1.Attributes["onchange"] = callbackRef;
}
private bool IsCallback()
{/*function to recognize callback */ ............}

The trick in implementing the callback is in how your client scripts call the server method. So, the following will be included in the .aspx file.

function HandleCallBack()
{
   var EmpID = document.all["TextBox1"].value;
   var xmlRequest = DoCallback("webform1.aspx", EmpID);
   if (xmlRequest.ResponseText=="true")
   {
   alert("user validated from backend");
   window.document.forms(0).TextBox2.focus();
   }
   else
   {
   alert("userid not found");
   window.document.getElementById("TextBox1").focus();
   }
}

Now, if you go through the documentation provided for script callback, you will see that WebForm_DoCallback uses a COM object to issue an HTTP POST or GET command to the specified target URL. That is, it uses the xmlhttp.

Example:

var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");

The HTTP verb is GET or POST depending on the size of the data to send. If the size exceeds 2 Kb, a POST command is used. The HTTP request consists of three logical elements: __CALLBACKID, __CALLBACKPARAM, and posted data. The __CALLBACKID value contains the destination URL (the event target parameter), whereas __CALLBACKPARAM carries the input parameter for the server-side stub method. The posted data is collected by the WebForm_InitCallback method and appended to the HTTP command. Thus, all you need is JavaScript code to inject into all pages that intend to support client callbacks. This code should initiate and control the remote URL invocation.

So, the core is your callback.js JavaScript files that have functions to handle the callback and returning the response from server to client:

function WebForm_DoCallback(eventTarget, eventArgument, eventCallback,
                            context, errorCallback)
{......}

function WebForm_CallbackComplete()

function WebForm_InitCallback(theForm)

The Server Processes the Callback

Because the server got the request through the “Microsoft.XMLHTTP,” it needs to process the same and return the result back to the client script. Here is the code to accomplish that:

string RaiseCallbackEvent(string eventArgument)
{//validate the userid from backend where userid = eventArgument.....
}

Handle the Response from the Server for the Callback

Below is the client script code to display the result to the user whether the userid is found or not:

function HandleCallBack()
{
   var EmpID = document.all["TextBox1"].value;
   var xmlRequest = DoCallback("webform1.aspx", EmpID);
   if (xmlRequest.ResponseText=="true")
   {
      alert("user validated from backend");
      window.document.forms(0).TextBox2.focus();
   }
   else
   {
      alert("userid not found");
      window.document.getElementById("TextBox1").focus();
   }
}

So, in a simple step you have successfully implemented a script callback in ASP.NET 1.1. The complete code sample is available in zip format to download in the code link below.

Conclusion

Script callback is an important yet lesser used feature that can be useful in many scenarios and comes in handy with proper usage. You can find the same in extensive use in the ASP.NET 2.0 Framework. The basis of the script callback that is the xmlhttp is used quite a bit now, also in a search engine, sending page data through client script, and in getting the server data with out post back. The usefulness of the same in .NET framework is it hides the XML DOM’s HTTP API exposed by microsoft.xmlhttp from you through built-in classes for the same.

I hope that you found this article useful. For any comments/suggestion, I can be reached at sndshreeman@yahoo.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read