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.



About the Author

shreemannarayana dash

shreeman narayana dash is a .net architect.His area of interest are distributed programming,Webservice,Remoting and serialization and Network technologies . you can reach shreeman at sndshreeman@rediffmail.com

Downloads

Comments

  • How can i get a data from the server using callback

    Posted by wafiNET on 06/12/2005 12:12pm

    I want to retrive a data from the server using this method. i have tow dropdownlists on mypage the first one filled on the page load and the secound one must be filled when the the first one value changed i want to execute an SQL Statment and fill the secound list without pastback to the server.

    • Re:Populate the dropdown

      Posted by sndshreeman on 07/21/2005 02:33am

      If you have gone thru the code you would have find that behind the scene the textbox data is being passed to BackEnd for validation and after the result was validated the result from BE is passed back in format var xmlRequest = DoCallback("webform1.aspx", EmpID); if (xmlRequest.ResponseText=="true") { alert("user validated from backend"); window.document.forms(0).TextBox2.focus(); } Thus in dropdown scenario you need to set the source of your dropdown to this xml that is xmlRequest.Responsetext.That is you ll get the xml as the callback result and bindign is on you to what control you wants to bind the same. Soon i am going to provide the third articles in this series in few days .This time i ll do the same with a workign dropdown example.

      Reply
    • Good example

      Posted by werds on 07/20/2005 09:01am

      How would you modify the code to populate a dropdown.

      Reply
    • Re:data from the server using callback

      Posted by sndshreeman on 06/13/2005 02:18am

      IF you have gone thru the article that is exactly what done here.In the example : the date in textbox data validated from the backend server thru callback not postback.the code is also given as download.. you can modify the code to achieve ur functionality by change the query and the handle call back var xmlRequest = DoCallback() now from this loop object you can add ur data to the combo.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds