|
Comments
|
Today's Top SOA Links
ASP.NET ASP.NET 2.0 Callbacks
Using callbacks to create a rich Web UI
By: Timothy Stall
Mar. 17, 2006 08:30 AM
Web sites are based on a client-server model. While the client (usually a browser) can use client-side script such as JavaScript to do simple tweaking of existing data, getting new data requires a request to the server. The server does the heavy processing and makes a response back, which redraws the entire Web page. Most development required these interactions to be atomic so that each request to the server returns a completely new page.
An Overview of Callbacks
Let's go over the technical implementation of the simplest callback, and then show both some advanced features as well as the limitations of callbacks. First, any page with callbacks must implement System.Web.UI.ICallbackEventHandler (see Listing 1). This has two methods:RaiseCall-backEvent and GetCallbackResult. One approach is to use RaiseCall-backEvent for internal plumbing and let GetCallbackResult do the real work. RaiseCallbackEvent is fired first; it receives the client side data, and stores it in a member variable. GetCallbackResult is fired second; it accesses the client data via the member variable, does whatever manipulation you need, and then returns the new string. This return value will be passed to the receiving client function. Still in the CodeBehind, we need to do some plumbing to register the client-side scripts (see Listing 2). We need to register two client scripts: ReceiveCallback, which receives the callback when returning from the server, and CallServer, which calls the server (the names are intended to be obvious). These steps must always be run, not just in a "not PostBack" block, so we can abstract them to a method appropriately named "SetupAlways," and call that in the page load. Meanwhile, at the client we add two simple scripts: StartCallback and ReceiveCallback (see Listing 3). StartCallback is a custom function written by the developer whose sole purpose is to invoke CallServer and pass in a string of data. You can trigger it however you want, pass in any parameters you want, assemble the data however you want, and trigger the CallServer function based on whatever logic you want. The CallServer function itself was automatically created in the previous step. ReceiveCallback is always triggered upon returning from the callback. We indicated that this was the receiving function when we registered it at the server. It expects a string that it can use for any JavaScript purpose - such as setting a message or modifying the DOM. At this point, we have the full life cycle of a callback implemented. Let's build off of this with some more advanced topics.
Multiple Callbacks on a Single Page Figure 1 illustrates this concept. First we have multiple client functions (StartA, StartB), which both call CallServer function [Step 1], but prefix the data string with an "A" or "B," respectively. The CallServer method still triggers the GetCallbackResult method on the server [Step 2], but now this method strips off the first character and uses it in a switch-case to call either ProcessA or ProcessB [Step 3]. Both processes return a string, but again prefix it with "A" or "B," and return it to the client [Step 4]. Finally, the client ReceiveCallback function strips off the first letter, and uses it to call the appropriate client receiving function, ReceiveA or ReceiveB [Step 5]. You can check the code download for an example of this. (Source code is available by viewing the article online in the DNDJ archives, http://dotnet.sys-con.com/read/issue/archives/, Vol. 4 iss. 3.)
Tips For Better Callbacks First, UserControls can implement callbacks just like pages! This lets you incorporate callbacks into your reusable controls. Second, while callbacks can't set the Response, they can still access all server resources such as the database, session, and page's viewstate. For example, suppose you want a callback to return a list of employees, excluding the current user. If the current user info was stored in session or viewstate, you could access that and manipulate the data appropriately at the server. This is helpful because it's easier to manipulate data on the server rather than the client due to the server's having better data structures and testability. Third, often you'll want user interactions to change the page such as adding or removing table rows. For example, in postbacks a button click does this by re-creating HTML for the entire page. However in a callback, you keep the existing page, and therefore achieve this by modifying the DOM. Therefore you'll want to create client script libraries for easily modifying the DOM - such as adding or removing rows, hiding elements, or updating status messages. In some cases, you can have the server create the HTML for you, return that as the string, and then set the innerHTML property of a span or div. Last, when we collect data to send to the server, it's often not just a single, primitive, string type. Rather, there are other data types (int, bool, dates), multiple values, and sometimes even a collection of objects that we need to send back to the server. For example, you may have an employee object with an ID (int) and Name (string) and you may want to send a group of these in the callback. One relatively easy way to handle this is with XML serialization. This has four steps:
You can use the built-in XML serialization methods to write a quick utility (see Listing 5). You can then easily assemble the necessary XML via JavaScript (Listing 6). Finally, you can deserialize at the server with the utility method shown in Listing 7.
Summary Having this technique in your arsenal will fundamentally change how you develop, open up new doors that you wouldn't think possible before, and save you tons of time and performance. Given how easy callbacks are to implement, it's a technique well worth learning. Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||