Prev | Current Page 514 | Next

Rob Cameron and Dale Michalk

"Pro ASP.NET 3.5 Server Controls and AJAX Components"

The GetNews and LoadNews functions are
similar in functionality to the previous web form callback example.
private void CreateClientScript()
{
list.Attributes.Add("onChange",
"GetNews(this.options[this.selectedIndex].value)");
string clientRecFunc = @"
function LoadNews(results, context)
{
var newsDiv = document.getElementById('$div');
newsDiv.innerHTML = results;
}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"LoadNews", clientRecFunc.Replace("$div",div.ClientID),
true);
string callBack = Page.ClientScript.GetCallbackEventReference(
this, "symbol", "LoadNews", null);
string clientCallFunc = "function GetNews(symbol){ " +
callBack + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"GetNews", clientCallFunc, true);
}
A bigger difference in the control callback implementation is evident in the code that
handles the callback invocation in the control server-side code. The result returned is sent
back in HTML form for the client to insert into the div element below the DropDownList control.
The call to GetNewsItem does all the work to retrieve and parse the RSS feed into a generic List
collection of NewsItem-based objects that are serialized into the HTML string.
public void RaiseCallbackEvent(string eventArgument)
{
items = GetNewsItems(eventArgument);
}
public string GetCallbackResult()
{
StringBuilder sb = new StringBuilder();
if (items != null)
{
foreach (NewsItem item in items)
{
sb.


Pages:
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526