Friday, August 19, 2005

Changes in Client Callback in Visual Studio 2005 July CTP

If you use the July CTP of Visual Studio 2005, you might have noticed that the RaiseCallbackEvent event (in the ICallbackEventHandler interface) has now been split into two separate events:

  • PrepareCallbackEvent

  • RenderCallbackResult
That means that the Client Callback example in my book - ASP.NET 2.0: A Developer’s Notebook, will break. Fortunately, a reader named Cindy Kee alerted me to the problem and helped to change the code listed on page 266 and 267 of the book to use the two new events. Here they are, in C#:

void ICallbackEventHandler.PrepareCallbackEvent(string eventArgument)
{
this._eventArg = eventArgument;
}

string ICallbackEventHandler.RenderCallbackResult()
{
if (this._eventArg.StartsWith("1:"))
{
//--strips away the command
this._eventArg = this._eventArg.Substring(2);
//--get city and state based on Zipcode
switch (this._eventArg)
{
case "95472":
return "Sebastopol,CA";
case "02140":
return "Cambridge,MA";
default:
throw (new Exception("ZipCode not valid!"));
}
}
else if (this._eventArg.StartsWith("2:"))
{
//--strips away the command
this._eventArg = this._eventArg.Substring(2);
//--get states and cities related to country
switch (this._eventArg)
{
case "Sing":
return "Singapore,";
case "US":
return "Alabama,California,Maryland,Massachusetts,New York,Oklahoma,Wisconsin,";
case "UK":
return "Birmingham,Cambridge,Christchurch,Leeds,Sheffield,";
default:
return "";
}
}
else
{
return "Command not recognized";
}
}

In essence, the two events above replace the original function that handled the RaiseCallbackEvent event. The rationale for the changes is to ensure callbacks work with asynchronous data sources.

For VB2005 readers, it should not be difficult to modify the original code in the book. Thanks, Cindy, for pointing that out to me!

No comments: