|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionOne of the challenges that every ASP.NET 1.x web developer faced is that there isn't any native support for script callbacks to server without post-back of the current page. For example, retrieving and presenting the details of a selected employee in a dropdown list without forcing the full page refresh, and this is particularly an expensive call for UI-rich pages. So, how can we handle this? Thanks to the ASP.NET team, they've built in this feature in ASP.NET 2.0, which allows calls to server events from client code without causing the page to post back and refresh. This is great, but we have to consider 2 facts: Time and Money. For time, we have to wait until ASP.NET 2.0 is released and we can't do anything in the meanwhile. For Money, we may be forced to upgrade Visual Studio .NET IDE from 2002/2003 to 2005 if we can't live without the Visual Studio.NET IDE. More importantly, implementing the script callbacks in ASP.NET 1.x isn't that hard at all. So, what are we waiting for? Thanks to the extensible architecture of Page Controller framework, it gives developers the flexibility in hand to extend its functionalities, where native support is not sufficed. In this article, I'll walk you through the process of implementing the Script Callbacks Framework, and also provide a simplified but effective framework that you can use in ASP.NET 1.x. ObjectivesSeveral objectives have been set for the implementation of Script Callback Framework in ASP.NET 1.x, they are:
BackgroundScript Callback Framework consists of 2 core components, which are PageTemplate.cs and ScriptCallback.js files. Each of them takes care of the callbacks processing at server side and client side, respectively. The steps involved in callbacks processing are shown below:
Script preparation
/// <summary>
/// Obtains a reference to a client-side script function
/// that causes, when invoked,
/// the server to callback asynchronously to the page.
/// This method also passes a parameter to the server control
/// that performs the callback processing on the server.
/// </summary>
/// <param name="control">The server control to process the callback.
/// It must implement the IClientCallbackEventHandler</param>
/// <param name="args">Argument to be passed to the server control
/// that performs the callback processing on the server</param>
/// <param name="cbHandler">Callback Handler (JavaScript function),
/// invokes when callback operation completed successfully</param>
/// <param name="context">Any DHTML reference or extra information
/// to pass to the Callback or Error Handler</param>
/// <param name="errHandler">Error Handler (JavaScript function),
/// invokes when error occurred during the callback operation</param>
/// <returns>Asynchronous callback JavaScript code</returns>
public string GetAsyncCallbackEventReference(
System.Web.UI.Control control,
string args,
string cbHandler,
string context,
string errHandler)/// <summary>
/// Obtains a reference to a client-side script function that causes,
/// when invoked, the server to callback synchronously to the page.
/// This method also passes a parameter to the server control
/// that performs the callback processing on the server.
/// </summary>
/// <param name="control">The server control to process
/// the callback. It must implement the IClientCallbackEventHandler</param>
/// <param name="args">Argument to be passed to the server
/// control that performs the callback processing on the server</param>
/// <param name="cbHandler">Callback Handler (JavaScript function),
/// invokes when callback operation completed successfully</param>
/// <param name="context">Any DHTML reference or extra information
/// to pass to the Callback or Error Handler</param>
/// <param name="errHandler">Error Handler (JavaScript function,
/// invokes when error occurred during the callback operation</param>
/// <returns>Synchronous callback JavaScript code</returns>
public string GetSyncCallbackEventReference(
System.Web.UI.Control control,
string args,
string cbHandler,
string context,
string errHandler)
For example, the following call to Server Side code// Callback asynchronously when the button is clicked
IncreaseButton.Attributes["onclick"] = GetAsyncCallbackEventReference
(
Form1,
String.Format("document.getElementById('{0}').value", txtValue.UniqueID),
"IncreaseValueHandler",
String.Format("document.getElementById('{0}')", txtValue.UniqueID),
null
);
Client Side code<input name="IncreaseButton" id="IncreaseButton"
type="button" value="Increase Value"
onclick="javascript:WebForm_DoAsyncCallback('Form1',
document.getElementById('txtValue').value,
IncreaseValueHandler,
document.getElementById('txtValue'),
null);" />
Script callbacksWhenever the control's event is fired, its embedded JavaScript code invokes the var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
The HTTP verb is GET or POST depending on the size of the data to be sent. If the size exceeds 2KB, a POST command is used. The HTTP request consists of three logical elements: Server responses
Internally, it uses the value obtained from The string RaiseClientCallbackEvent(string eventArgument);
The
Note: The status code is appended into a custom HTTP Header entry named " Callback handlingOnce a callback operation has completed, the client script will be notified to check the status code returned from the custom HTTP Header entry named " Using the codeWith Script Callback Framework in place, developing a Page that makes use of script callbacks is as easy as the few steps listed below:
In short, you can use callbacks to update individual elements of a page, such as a Last, but not least, I've included a sample, available from the link at the top of this article, that demonstrates the capability of Script Callback Framework. Points of InterestThroughout the numerous testing on Script Callback Framework, I found that ConclusionScript callbacks allow you to perform callbacks to the server without refreshing the whole page, which gives users the illusion that everything is taking place on the client. Once again, I have presented the Script Callback Framework which is a simplified version of code that will be completely built when ASP.NET 2.0 is released. You might as well get the functionality you want today, and you'll be ahead of the curve in understanding this similar concept in ASP.NET 2.0 tomorrow. | ||||||||||||||||||||||||||||||