Web pages that shoot out-of-band calls need to have one or more trigger events that, when properly
handled with a piece of JavaScript code, place the request via the XMLHttpRequest object.
Trigger events can only be HTML events captured by the browser’s DOM implementation.
The JavaScript code should initiate and control the remote URL invocation, as shown in the
following code:
The sample function accepts two strings: the URL to call and the parameter list. Note that the
format of the query string is totally arbitrary and can be adapted at will in custom implementations.
The URL specified by the programmer is extended to include a couple of parameters.
The first parameter—named outofband in the example—is a Boolean value and indicates
whether or not the request is going to be a custom callback request. By knowing this, the target
page can process the request appropriately. The second parameter—named param in the
example—contains the input parameters for the server-side code.
The host ASP.NET page looks like the following code snippet:
Testing Out-of-band
The code-behind class is shown in the following listing:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsOutOfBand())
return;
if (!IsPostBack)
PopulateList();
}
private bool IsOutOfBand()
{
bool isCallback = false;
isCallback = String.Equals(Page.Request.QueryString["callback"],
"true",
StringComparison.OrdinalIgnoreCase);
if (isCallback)
{
string param = Request.QueryString["param"].ToString();
Response.Write(ExecutePageMethod(param));
Response.Flush();
Response.End();
return true;
}
return false;
}
private void PopulateList()
{
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT employeeid, lastname FROM employees",
"SERVER=(local);DATABASE=northwind;UID=...;");
DataTable table = new DataTable();
adapter.Fill(table);
EmployeeList.DataTextField = "lastname";
EmployeeList.DataValueField = "employeeid";
EmployeeList.DataSource = table;
EmployeeList.DataBind();
}
string ExecutePageMethod(string eventArgument)
{
return "You clicked: " + eventArgument;
}
}
A couple of issues deserve more attention and explanation. The first one is the need to find
out whether the request is an out-of-band call or a regular postback. Next, we need to look at
the generation of the response. The IsOutOfBand method checks the outofband field in the
posted form. If the outofband field is found, the request is served and terminated without going
through the final part of the classic ASP.NET request life cycle—events for changed values,
postback, pre-rendering, view-state serialization, rendering, and so forth. An out-of-band
request is therefore short-circuited to return more quickly, carrying the least data possible.
What does the page do when an out-of-band call is placed? How does it determine the
response? Most of the actual AJAX-based frameworks vary on this point, so let’s say it is arbitrary.
In general, you need to define a public programming interface that is invoked when an
out-of-band call is made. In the sample code, I have a method with a contracted name and
signature—ExecutePageMethod—whose output becomes the plain response for the request. In
the sample code, the method returns and accepts a string, meaning that any input and output
parameters must be serializable to a string.
string param = Request.QueryString["param"].ToString();
Response.Write(ExecutePageMethod(param));
Response.Flush();
Response.End();
As in the code snippet, the response for the out-of-band request is the output of the method.
No other data is ever returned; and no other data except for the parameters is ever sent. In this
particular implementation, there will be no view state sent and returned.
Important Although you’ll probably never get to write any such code, be aware that thus
far I’ve just provided a minimal but effective description of the underlying mechanism common
to most frameworks that supply AJAX-like functionality. Each framework encapsulates a
good number of details and adds new services and capabilities. At its core, though, this is
how AJAX libraries work.